TomWayne
TomWayne

Reputation: 230

Filling a NSMutableArray not working

Simple question but I don't get it. I defined a

 @property NSMutableArray* Checkmarks; 

in myTableView.h

and @synthesize it in myTableView.m In my table view I allocated it with

Checkmarks = [[NSMutableArray alloc] init]; 

in viewDidLoad() In didSelectRowAtIndexPath the Array is filled with

[Checkmarks addObject:myObject]; 

but my log says that Checkmarks is empty though myObject is not.

This only occurs in iOS9 but I don't know whats causing this or what changed.

Please help :)

Upvotes: 0

Views: 56

Answers (2)

Tiago Almeida
Tiago Almeida

Reputation: 14237

First of all you don't need synthesize, you only need it when you provide a custom setter AND getter, and it seems that is not your case.

Second you should declare your properties with explicit memory ownership, therefore, you should have:

@property (strong, nonatomic) NSMutableArray *checkmarks;

When you remove your synthesize you can either do _checkmarks addObject or [self.checkmarks addObject:myObject].

Then try to print your checkmarks before and after your addObject. Checkmark has to print an empty array before the addObject and then has to have the object. Note that an empty array isn't the same as nil.

Upvotes: 3

Stephen Darlington
Stephen Darlington

Reputation: 52575

Since Checkmarks is nil, it means that your alloc/init didn't work as you expected.

Your viewDidLoad should look something like this:

-(void)viewDidLoad {
  [super viewDidLoad];
  self.Checkmark = [[NSMutableArray alloc] init];
}

This should be in a UIViewController (including a UITableViewController) and not in a UITableView.

(Also worth noting: by convention, properties begin with a lower case letter and, as Tiago says, you should be explicit with your ownership, though I think the defaults mean this isn't your issue here.)

Upvotes: 2

Related Questions