代码之家  ›  专栏  ›  技术社区  ›  Cocoa Dev

关于nsdictionaries的一个重要问题

  •  0
  • Cocoa Dev  · 技术社区  · 15 年前

    我有一本带nsstrings的nsdictionary

    一些valueforkey:@“key”没有条目,所以它是(空)

    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,但是过滤掉那些空的键。有什么想法吗?

    更新:::::

    基本上我希望得到的字符串看起来像

    1个无限循环,Cupertino,CA,95014(如果所有字段都可用)

    如果我错过了那条街

    加利福尼亚州库比蒂诺,95014

    如果我错过了州,那么

    1个无限循环,Cupertino,95014

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

    加利福尼亚州

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

    2 回复  |  直到 15 年前
        1
  •  1
  •   Joshua Nozzi    15 年前

    检查是否有该键的值如何?

    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.

    相反,您可以尝试以下方法:

    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.

        2
  •  0
  •   TechZen    15 年前

    不完全确定你想做什么,但这是:

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

    …印刷品:

    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.