Parse NSDictionary for custom delimited string

I have an NSMutableDictionary with some values ​​in it, and I need to combine the keys and values ​​into a string, so

> name = Fred > password = cakeismyfavoritefood > email = myemailaddress@is.short 

becomes name=Fred&password=cakeismyfavoritefood&email=myemailaddress@is.short

How can i do this? Is there a way to join NSDictionaries in strings?

+7
join iphone nsstring separator nsdictionary
source share
2 answers

You can easily do this by listing the dictionary keys and objects:

 NSMutableString *resultString = [NSMutableString string]; for (NSString* key in [yourDictionary allKeys]){ if ([resultString length]>0) [resultString appendString:@"&"]; [resultString appendFormat:@"%@=%@", key, [yourDict objectForKey:key]]; } 
+14
source share

Just the same question as Including NSDictionary in a string using blocks?

 NSMutableArray* parametersArray = [[NSMutableArray alloc] init]; [yourDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [parametersArray addObject:[NSString stringWithFormat:@"%@=%@", key, obj]]; }]; NSString* parameterString = [parametersArray componentsJoinedByString:@"&"]; [parametersArray release]; 
+6
source share

All Articles