0

我是 ios 新手,正在尝试做这样的事情

self.dataArray = [dictionary valueForKey:@"LIST"];

{
  "List": [
    {
      "FULLNAME": "FirstName, LastName",
      "ID": "281"
    }
  ]
}

我想交换“全名”中的值,即我希望它是姓氏,名字

我尝试了各种方法,但都行不通。有人能告诉我如何做到这一点吗?

谢谢

4

2 回答 2

3

假设您知道如何进行字典操作,这里有一种交换字符串中元素的方法:

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);
于 2012-10-16T10:57:02.253 回答
1

如果您的字典包含数组,我相信它应该是 objectForKey 而不是 valueForKey。

self.dataArray = [dictionary objectForKey:@"LIST"];
    for(NSString *fullname in self.dataArray){
        if([fullname isEqualToString:@"FULLNAME"]){
            NSArray *name = [fullname componentsSeparatedByString:@","];
            NSMutableString* reverseName = [NSMutableString string];
            for (int i=[name count]-1; i>=0;i--){
                [reverseName appendFormat:@", ", [name objectAtIndex:i]];
                //reverse name should be what you are looking for..or something close
            }
        }
    }

希望这可以帮助

于 2012-10-16T11:05:40.853 回答