在Objective-C逗号分隔字符串的NSArray [英] Comma-separated string to NSArray in Objective-C
问题描述
所以我有数组没有经验......但我需要用一个来填充一个UIPickerView。我获得通过HTTP(NSURLConnection的)对象的列表。这工作得很好。目前,响应存储在一个的NSString作为一个逗号分隔的列表。我需要将其转换为一个数组。我觉得这是数组的我需要的类型:
So I have no experience with arrays... But I need to use one to populate a UIPickerView. I am obtaining a list of objects via HTTP (NSURLConnection). This works fine. Currently, the response is stored in a NSString as a comma-separated list. I need to convert it to an array. I think this is the type of array I need:
NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];
也许我过于复杂的事情......我真的不知道。目前已为PickerView一个数组,我有这样的设置将项目添加到PickerView阵列我用这个code:
Maybe I'm overcomplicating things... I'm really not sure. There is already an array for the PickerView, and I have it setup so to add an item to the PickerView array I use this code:
[pickerArray addObject:@"Item 1"];
所以...我如何在一个逗号分隔的字符串(第1项,第2项,第3项,...)到单独的数组项分隔这些项目([pickerArray ADDOBJECT:@项目1];) ??
So... How do I separate the items in a comma-separated string (item 1,item 2,item 3,...) into separate array items ([pickerArray addObject:@"item 1"];)??
推荐答案
假设没有关于转义/包含反向转义的字符串中的逗号担心,它应该是这个简单的:
Assuming there's no worry about escaping/unescaping commas contained within the strings, it should be this simple:
NSArray *items = [theString componentsSeparatedByString:@","];
这篇关于在Objective-C逗号分隔字符串的NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!