假設你知道該怎麼做的字典操作,這裏的交換字符串中的元素的方法:
NSString* name = @"Firstname, Lastname";
// Remove the space after comma (not necessary if you know the name will always
// have a space after comma, then just split on ', '.
NSString* normalizedName = [name stringByReplacingOccurrencesOfString:@", " withString:@","];
// Split the string on ','.
NSArray* nameParts = [normalizedName componentsSeparatedByString:@","];
// Reverse the array
NSArray* reverseNameParts = [[nameParts reverseObjectEnumerator] allObjects];
// Join the array with ', '
NSString* revname = [reverseNameParts componentsJoinedByString:@", "];
NSLog(@"Reversed name parts: %@", revname);
我怎麼把它放回字典? – Hades
要回答這個問題,我需要更多地瞭解字典。通常,確保字典是可變的,然後使用'[dict setObject:revname forKey:@「FULLNAME」]'(在從數組和外部字典中提取它之後)。 – Krumelur
完美,謝謝! – Hades