Reputation: 2473
My NSMutableArray doesn't store any object, either a String or a complex class. I tried it to allocate ind init and to init as an array. Both without success.
.m file:
@interface WorkCentersViewController()
@property (nonatomic, retain) NSMutableArray *selectedWorkCenters;
@end
@implementation WorkCentersViewController
@synthesize selectedWorkCenters;
-(id)init{
self = [super init];
if(self){
//self.selectedWorkCenters = [[NSMutableArray alloc] init]; //doesn't work too
self.selectedWorkCenters = [NSMutableArray array];
}
return self;
}
- (void)dealloc
{
[selectedWorkCenters release];
[super dealloc];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
...
}else{
//select
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//add
[self.selectedWorkCenters addObject:[self.workCenters objectAtIndex:indexPath.row]];
NSLog(@"Sel WC Count: %d", [self.selectedWorkCenters count]); //always 0
}
}
Why is it not storing my object of WorkCenter? What I'm doing wrong?
BR, mybecks
Upvotes: 0
Views: 252
Reputation: 2473
I have found the error.
In a other method I had the assignment
selectedWorkCenters = availableWorkCenters;
availableWorkCenters wasn't initialized - always 0x0 in the debugger screen.
Upvotes: 0
Reputation: 40030
Ok so here are my ideas. In your init you are using:
// This one is wrong, you are [NSMutableArray array] returns a NSArray which cannot be
// mutated so you cannot add objects to it. That is why it is not working.
//
self.selectedWorkCenters = [NSMutableArray array];
// This one is wrong because you have a leak here (Although it should work)
//
self.selectedWorkCenters = [[NSMutableArray alloc] init];
Try this:
selectedWorkCenters = [[NSMutableArray alloc] init];
And place it in the viewDidLoad method. Hope it helps. Let me know :)
Upvotes: 0
Reputation: 735
Try initializing your NSMutableArray in this way:
self.selectedWorkCenters = [[NSMutableArray alloc] init];
Upvotes: 0
Reputation: 13381
-initWithNibName:bundle:
is UIViewController
's designated initializer, so any init code should show up in an override of that method. If this is a subclass of UITableViewController
, Mark is correct that initWithStyle
is a good place to put it.
Also, in general, viewDidLoad
/viewDidUnload
give you a good place to alloc/dealloc data structures that your view needs, but that can be recreated if needed.
Upvotes: 0
Reputation: 2096
Perhaps that specific init method isn't being called? Is this a UITableViewController? Maybe you're initializing it with initWithStyle:
? I'd set a break point on the line where you init the array just to check.
Upvotes: 1