Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

arrays - Comma-separated string to NSArray in Objective-C

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];

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"];

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"];)??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming there's no worry about escaping/unescaping commas contained within the strings, it should be this simple:

NSArray *items = [theString componentsSeparatedByString:@","];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...