开发者

NSDictionary - List all

In a NSMutableDictionary like this:

[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithString:firstName], @"NAME",
[NSNumber numberWithFloat:familyName], @"SURNAME",
nil];

How can all of its elements be converted to the NSString? with the requirement that names and surnames are arranged in string like this: "name, surname, name, surname..."?

This gives a string with all the names but not surnames:

NSString * result = [[urlArray valueForKey:@"NAME"] componentsJoinedByString:@", "];

Is there 开发者_StackOverflow中文版a way similar to the one above to create a string with all the values of NSMutableDictionary?


Try something like this:

NSString *result = [NSString stringWithFormat:@"%@, %@", [urlArray valueForKey:@"SURNAME"], [urlArray valueForKey:@"NAME"]];

The %@ represents an objective-c object for more details see the Apple docs


The code to make a NSString from an NSDictionary may look like (if you're trying to print out everything in the NSDictionary)

NSMutableString* mutableString = [NSMutableString string];
NSDictionary* dictionary;

for(NSString* key in [dictionary allKeys]) 
{
    [mutableString appendString:[[dictionary objectForKey:key] description]];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜