0

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

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I call it from the RootViewController: - (void) insertNewWorkplace { NSLog(@"Add new workplace"); WorkCentersViewController *workCentersController = [[WorkCentersViewController alloc] init]; [self.navigationController pushViewController:workCentersController animated:YES]; [workCentersController release]; } Yes it is a TableViewController. The init Method is called, I tested it with a Brreakpoint before.
Set a breakpoint or use NSLog in the init just to make sure. Also I'd make certain that the object you are trying to add to the array isn't nil.
My Object is not null, I test it with NSLOg. My output of NSLog in viewDidLoad is ( ).
0

-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.

2 Comments

I added my init code of the NSMutableArray to the viewDidLoad method. But won't works either with [[... alloc] init] and [NSMutableArray array]
NSLog both arrays (workCenters and selected...) and see what you find. Perhaps workCenters is nil?
0

Try initializing your NSMutableArray in this way:

self.selectedWorkCenters = [[NSMutableArray alloc] init];

Comments

0

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 :)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.