NSMutableArray与NSArray [英] NSMutableArray vs NSArray
问题描述
我创建了一个脚本,从服务器加载XML解析后将其保存到NSMutableArray
i have created a script, witch load the XML from server parse it an save it to NSMutableArray
[kategorienArray addObject:catName];
catName包含一个字符串,如果我使用NSLog Array可以正常工作。
catName ist a String, if i NSLog the Array everything works fine.
之后,我创建了一个tebleview,然后重新加载数据
After that i have created a tebleview, and reload Data
[kategorienAuswahl reloadData];
KategorienAuswahl是我的TableView
KategorienAuswahl is my TableView
如果我使用普通数组,则会得到问题
and now a get the Problems
NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
self.listData = array;
[array release];
将会显示,但如果我使用
the will be displayed, but if i use
cell.textLabel.text = [kategorienArray objectAtIndex:row];
我得到EXC_BAD_ACCESS
i get EXC_BAD_ACCESS
相反
cell.textLabel.text = [listData objectAtIndex:row];
我添加nwo
kategorienArray = [[NSMutableArray alloc] init];
kategorienArray = [NSMutableArray arrayWithCapacity:10];
但现在我的Tableview中没有数据
but now i get no data in my Tableview
推荐答案
您可能尚未初始化 kategorienArray
。
您的代码中是否有 kategorienArray = [[NSMutableArray alloc] init];
?您应该先完成此操作,然后再向其中添加对象。
You might not have initialized your kategorienArray
.
Do you have a kategorienArray = [[NSMutableArray alloc] init];
in your code? You should have done this before adding objects to it and then accessing them.
此外,请确保将数组保留在创建点处,以便系统不会在访问其项目之前,请不要回收其内存。我用来确保一切正常的方法是将可变数组声明为属性。在您的ViewController的.h文件中
Also, make sure your array is retained at the point where it's created, so that the system doesn't reclaim its memory before you access its items. The way I use to make sure things are going right is to declare the mutable array as a property. In your viewcontroller's .h file put
@property (nonatomic, retain) NSMutableArray *kategorienArray;
,然后在您的.m文件中放入
and in your .m file put
@synthesize kategorienArray;
在顶部,然后
self.kategorienArray = [NSMutableArray array];
在 viewDidLoad
或其他位置,然后添加项目
in viewDidLoad
or somewhere, before adding items to it.
这篇关于NSMutableArray与NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!