Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

objective c - NSMutableArray vs NSArray

i have created a script, witch load the XML from server parse it an save it to NSMutableArray

[kategorienArray addObject:catName];

catName ist a String, if i NSLog the Array everything works fine.

After that i have created a tebleview, and reload Data

[kategorienAuswahl reloadData];

KategorienAuswahl is my TableView

and now a get the Problems

if i Use a "normal" array

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

i get EXC_BAD_ACCESS

instead it works fine with

cell.textLabel.text = [listData objectAtIndex:row]; 

I Add nwo

kategorienArray = [[NSMutableArray alloc] init];
    kategorienArray = [NSMutableArray arrayWithCapacity:10];

but now i get no data in my Tableview

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.

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;

and in your .m file put

@synthesize kategorienArray;

at the top and then

self.kategorienArray = [NSMutableArray array];

in viewDidLoad or somewhere, before adding items to it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...