我目前收到的字符串如下:

@"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54"

我把它分成这样:

testArray = [[NSArray alloc] init];
NSString *testString = [[NSString alloc] initWithFormat:@"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54,Steve|56,Matty|24,Bill|30,Rob|30,Jason|33,Mark|22,Stuart|54,Kevin|30"];
testArray = [testString componentsSeparatedByString:@","];

dict = [NSMutableDictionary dictionary];
for (NSString *s in testArray) {

    testArray2 = [s componentsSeparatedByString:@"|"];
    [dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]];
}

我现在将收到如下字符串:

@"Sam|26|Developer,Hannah|22|Team Leader,Adam|30|Director,Carlie|32|PA,Jan|54|Cleaner"

我能否(以及如何)使用与上述相同的方法,使用"|"分隔符多次分隔字符串?

推荐答案

下面一行...

testArray2 = [s componentsSeparatedByString:@"|"];

将使数组现在包含3项,而不是2项.....没必要再分手了!

Objective-c相关问答推荐

将 NSMutableAttributedString 转换为 NSString

UICollectionView:一行或一列

当 UITextField 成为第一响应者时如何使 UIScrollView 自动滚动

Objective-C 静态、外部、公共变量

如何将变量参数传递给另一个方法?

如何使用键值编码判断对象是否存在键?

在 iOS 设备上本地存储图像

改变 setContentOffset:animated: 的速度?

Objective-C:如何从子类访问父属性?

如何将 .plist 文件中的数据 struct 读入 NSArray

为什么 NSOrderedSet 不继承自 NSSet?

iOS 设备作为 Web 服务器

如何从 UIGestureRecognizer 获取 UITouch 位置

强制iOS应用程序崩溃的最快方法是什么?

以编程方式查找 iphone 的语言环境货币

如何创建 .dSYM 文件?

iPhone:如何确定线程是否是主线程?

移除所有 CALayer 的子层

无法从其 DataSource 获取单元格

@package 指令在 Objective-C 中的作用是什么?