逗号分隔的字符串到 Objective-C 中的 NSArray [英] Comma-separated string to NSArray in Objective-C

查看:36
本文介绍了逗号分隔的字符串到 Objective-C 中的 NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我没有使用数组的经验...但我需要使用一个来填充 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 数组添加一个项目,我使用以下代码:

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

推荐答案

假设不用担心转义/取消转义字符串中包含的逗号,它应该是这么简单:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆