objective-c - 如何使用 nsdictionary 拆分字符串和交换值

标签 objective-c ios

我是 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/

相关文章:

ios - UITableViewCell - 如何在计算 heightForRowAtIndex 之前向 UITextView 添加约束

ios - 应用程序未通过代码设计验证 (-19011)

objective-c - 有没有办法停止所有 RestKit 处理?

ios - 如何自动缩放 WKWebView 的内容?

ios - Swift 相当于 "[[transferManager download:downloadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task)"

ios - 从特定链接下载文件并将其存储在设备 iOS 中?

iphone - viewWilldisappear 和 viewDiddisappear 之间会发生什么?

iphone - UITextView 文本的颜色变化

ios - 以编程方式为给定 iTunes ID 的所有地区构建 iTunes URL?

objective-c - 如何在Mac上与其他用户共享iPhone应用程序?