关于 NSDictionaries 的一个重要问题

发布于 2024-09-06 17:28:07 字数 905 浏览 1 评论 0原文

我有一个带有 NSStrings 的 NSDictionary

一些 valueForKey:@"Key" 没有条目,所以它是 (null)

NSMutableString* addressDictionaryToString = [NSMutableString string];  // use mutable string!
for (NSDictionary* item in address) {     // use fast enumeration!
        [addressDictionaryToString appendFormat:@"%@, %@, %@, %@",
         [item objectForKey:@"Street"],         
         [item objectForKey:@"City"],
         [item objectForKey:@"State"],
         [item objectForKey:@"ZIP"]
         ];
    NSLog(@"MutableString: %@", addressDictionaryToString);
}

所以我想构建一个 NSMutableString 但过滤掉那些为 null 的键。有什么想法吗?

更新:::::

基本上我希望我的结果字符串看起来像

1 Infinite Loop, Cupertino, CA, 95014 (如果所有字段都可用)

如果我错过了街道,那么

Cupertino, CA, 95014

如果我错过了州,那么

1 Infinite Loop, Cupertino, 95014

如果我只有状态,那么它应该打印

CA

(请注意,最后一个元素上没有逗号)

I have an NSDictionary with NSStrings

Some of the valueForKey:@"Key" have no entry so it's (null)

NSMutableString* addressDictionaryToString = [NSMutableString string];  // use mutable string!
for (NSDictionary* item in address) {     // use fast enumeration!
        [addressDictionaryToString appendFormat:@"%@, %@, %@, %@",
         [item objectForKey:@"Street"],         
         [item objectForKey:@"City"],
         [item objectForKey:@"State"],
         [item objectForKey:@"ZIP"]
         ];
    NSLog(@"MutableString: %@", addressDictionaryToString);
}

So I want to build an NSMutableString but filter out those keys that are null. Any ideas?

UPDATE:::::

Basically I want my resulting String to look like

1 Infinite Loop, Cupertino, CA, 95014 (IF all fields are available)

If Im missing the Street then

Cupertino, CA, 95014

If I'm missing the State then

1 Infinite Loop, Cupertino, 95014

If I only have the state then it should print

CA

(notice that there are no commas on the last element)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2024-09-13 17:28:07

检查该键是否有值怎么样?

NSMutableString * addressDictionaryToString = [NSMutableString string];
for (NSDictionary * item in address)
{
    if ([item objectForKey:@"Street"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"Street"]];
    if ([item objectForKey:@"City"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"City"]];
    if ([item objectForKey:@"State"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"State"]];
    if ([item objectForKey:@"ZIP"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"ZIP"]];
    NSLog(@"MutableString: %@", addressDictionaryToString);
}

问题是,在你的上一个问题中,你说你的目标是创建一个 CSV 文件。如果您的行具有不同数量的字段,并且没有可靠的方法来识别每个字段,那么它在技术上就不是有效的 CSV。

相反,您可以尝试这样做:

NSMutableString * addressDictionaryToString = [NSMutableString string];
for (NSDictionary * item in address)
{
    [addressDictionaryToString appendFormat:@"%@,", 
     ([item objectForKey:@"Street"]) ? [item objectForKey:@"Street"] : @"" ];
    // ...
    NSLog(@"MutableString: %@", addressDictionaryToString);
}

它检查是否存在某个值,如果存在则插入该值,或者只是插入一个空字符串(结果为“值,值,,值...”)。另请记住,逗号后面不应该有空格,因此我已从本示例中删除了它。

How about checking if there's a value for the key?

NSMutableString * addressDictionaryToString = [NSMutableString string];
for (NSDictionary * item in address)
{
    if ([item objectForKey:@"Street"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"Street"]];
    if ([item objectForKey:@"City"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"City"]];
    if ([item objectForKey:@"State"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"State"]];
    if ([item objectForKey:@"ZIP"])
        [addressDictionaryToString appendFormat:@"%@, ", 
         [item objectForKey:@"ZIP"]];
    NSLog(@"MutableString: %@", addressDictionaryToString);
}

The thing is, in your last question, you said your goal was to create a CSV file. It's not technically valid CSV if your rows have varying numbers of fields with no reliable way to identify each one.

Instead, you might try this:

NSMutableString * addressDictionaryToString = [NSMutableString string];
for (NSDictionary * item in address)
{
    [addressDictionaryToString appendFormat:@"%@,", 
     ([item objectForKey:@"Street"]) ? [item objectForKey:@"Street"] : @"" ];
    // ...
    NSLog(@"MutableString: %@", addressDictionaryToString);
}

It checks for the presence of a value and inserts that value if there is one, or just an empty string (resulting in "value,value,,value..."). Also remember there shouldn't be a space after the comma, so I've removed that from this example.

沧笙踏歌 2024-09-13 17:28:07

不完全确定您要做什么,但是:

NSDictionary *d=[NSDictionary dictionaryWithObject:[NSNull null] forKey:@"ns"];
NSString *n=[@"Steve " stringByAppendingFormat:@"%@",[d objectForKey:@"ns"]];
NSLog(@"%@",n);

... prints:

Steve <null>

如果键本身不存在,那么当您尝试获取不存在的键的值时,它将抛出异常。在这种情况下,唯一的办法是在尝试使用每个字典中的键来检索值之前检查该键。

Not exactly sure what you're trying to do but this:

NSDictionary *d=[NSDictionary dictionaryWithObject:[NSNull null] forKey:@"ns"];
NSString *n=[@"Steve " stringByAppendingFormat:@"%@",[d objectForKey:@"ns"]];
NSLog(@"%@",n);

... prints:

Steve <null>

If the key itself does not exist then it will throw an exception when you try to get the value for the nonexistent key. The only recourse in this instance is to check for the key in each dictionary before trying to use it to retrieve a value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文