简体   繁体   English

使用componentsSeperatedByString问题将NSString转换为NSarray

[英]Converting NSString to NSarray using componentsSeperatedByString problem

First off I would like to thank everyone for attempting to solve my problem. 首先,我要感谢每个尝试解决我的问题的人。 I am kind of a noobie so any help will be appreciated. 我有点菜鸟,所以我们将不胜感激。

I have declared ( NSString *data ) that has retrieved content from a website. 我声明了( NSString *data )已从网站检索内容。 I have tested through various parts of my code that the data is in the string and that it is proper. 我已经通过代码的各个部分测试了数据是否在字符串中以及数据是否正确。

The data in the string is EXACTLY as follows: 字符串中的数据完全如下:

blah 2222, 3333, 1111 222 444 5555,<br/> blah 3333, 4444, 2222 333 555, <br/> blah 4444, 5555, 3333 444 666 blah 2222、3333、1111 222 444 5555,<br/> blah 3333、4444、2222 333 555,<br/> blah 4444、5555、3333 444 666

this pattern continues for a while. 这种模式持续了一段时间。 I want to parse this data in a array so it is separated by ",". 我想将此数据解析为一个数组,以“,”分隔。 Therefore; 因此; I declared an array called ( NSArray *storage ) and I will then do: 我声明了一个名为( NSArray *storage )的数组,然后我将这样做:

storage = [dat componentsSeperatedByString:","];

I then have a label called ( UILabel *label ). 然后,我有一个名为( UILabel *label )的UILabel *label It is connected through the interface builder, the property is set as well as synthesize. 它通过接口构建器连接,属性已设置并合成。

I then go: 然后我去:

label.text = [storage objectAtIndex:0];

I run the Iphone simulator and when i press the button in my app which will activate the change, the iphone simulator exists to the home screen where all the apps are located. 我运行Iphone模拟器,当我按应用程序中的按钮以激活更改时,iphone模拟器存在于所有应用程序所在的主屏幕上。 Why is this happening? 为什么会这样呢? why isn't it displaying blah 2222 ? 为什么不显示blah 2222

Thanks! 谢谢!

componentsSeparatedByString: returns an autoreleased array. componentsSeparatedByString:返回一个自动释放的数组。 Your storage array is probably already deallocated when you tap the button. 当您点击按钮时,您的storage阵列可能已经被释放。 Use a retained property for the array instead of assigning it directly to the instance variable. 对数组使用保留的属性,而不是直接将其分配给实例变量。

you can use this piece of code: 您可以使用以下代码:

NSString *data = @"blah 2222, 3333, 1111 222 444 5555,<br/> blah 3333, 4444, 2222 333 555, <br/> blah 4444, 5555, 3333 444 666";  
NSArray *storage = [data componentSeparatedBy:@","];  
NSString *first= [data objectAtIndex:0];  
NSString *second = [data objectAtIndex:1];
///... and so on
//or 
label.text = [data objectAtIndex:2];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM