我是 ios 的新手,正在尝试做这样的事情
self.dataArray = [dictionary valueForKey:@"LIST"];
{
"List": [
{
"FULLNAME": "FirstName, LastName",
"ID": "281"
}
]
}
我想交换“FULLNAME”中的值,即我希望它是 LastName,FirstName
我尝试了各种方法,但都不行。谁能告诉我如何实现这一点?
谢谢
最佳答案
假设您知道如何进行字典操作,下面是交换字符串中元素的一种方法:
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);
关于objective-c - 如何使用 nsdictionary 拆分字符串和交换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12913045/