问题描述
我正在尝试将NSMutableArray转换为字符串。
声明我的NSMutableArray ...
I have an NSMutableArray i am trying to convert into a string.
Declaring my NSMutableArray...
NSMutableArray *listData;
然后在一个方法中...
And later inside a method...
NSString *foo = [listData componentsJoinedByString:@"|"];
NSLog(@"%@",foo);
无论我尝试什么,我都不断获得EXC_BAD_ACCESS。
确保数组中的每个元素都是NSString我也尝试过此方法...
It seems no matter what i try i keep getting EXC_BAD_ACCESS.
To make sure each element in my array was an NSString i also tried this...
NSMutableArray *mArray = [[NSMutableArray alloc] init];
for (id ln in listData) {
NSString *boo = [NSString stringWithFormat: @"%@",ln];
[mArray addObject:boo];
}
NSString *foo = [mArray componentsJoinedByString:@"|"];
NSLog(@"%@",foo);
我可以通过在类内的同一方法或其他方法中添加/删除对象来操纵NSMutableArray。但是,当我尝试 componentsJoinedByString时,错误弹出。有没有人有任何建议或其他方法可以将这个数组合并为一个NSString?
I can manipulate my NSMutableArray by adding/deleting objects in the same method or other methods inside my class. But when i try "componentsJoinedByString" the error pops up. Does anyone have any advice or another way i can combine this array into a single NSString?
推荐答案
在您提供的代码中,永远不会有 NSMutableArray
表示 listData
。在代码中的某个时候,您需要创建一个代码,并大概填充它。
In the code you've given, there will never be an NSMutableArray
for listData
. At some point in your code, you'll need to create one, and presumably populate it.
编辑
好的,所以您可能会在这里遇到内存管理问题,因此让我们更加清楚:
EditOkay, so you may get into memory management problems here, so let's be a bit clearer:
您正在为实例变量合成getter和setter,因此使用它们来访问它是一个好习惯,它们将适当地保留和释放。
You're synthesizing getters and setters for the instance variable, so it's good practice to use those to access it, they'll take care of retain and releasing appropriately.
要设置 listData
,您只需使用
self.listData = [listManage getList:[[NSUserDefaults standardUserDefaults] stringForKey:@"list_name"] list:@"LIST"];
或
[self setListData:[listManage getList:[[NSUserDefaults standardUserDefaults] stringForKey:@"list_name"] list:@"LIST"]];
如果您愿意。
这篇关于componentsJoinedByString给了我EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!