NSMutableArray vs NSArray

2019-09-20 18:37发布

问题:

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

回答1:

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.



回答2:

The problem may be that 'kategorienArray' has been (auto)released. Is 'kategorienArray' an ivar ? Check out if when you created the array you correctly retained it (particularly if you created it with a class method).