0

I have a UITableView whose controller looks like this:

@interface FLWorkspaceViewController : UITableViewController

@property(nonatomic,retain) NSMutableArray* workspaces;

@end

I try to set that array in another controller:

for(NSDictionary* workspace in workspaces)
        {
            NSLog(@"%@",workspace);
            [workspaceViewController.workspaces addObject:workspace];
        }

        [self.navigationController pushViewController:workspaceViewController animated:YES];

I've checked the values and they exist. When I count workspaces in viewDidLoad, it's 0. Why is this?

jscs
  • 63,694
  • 13
  • 151
  • 195
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • make sure you have allocated memory to your workspaces array... – Ankit Srivastava Jun 10 '12 at 08:43
  • please check that workspaceViewController & workspaceViewController.workspaces is not nil – Omar Abdelhafith Jun 10 '12 at 08:43
  • where/when should i alloc the workspaces array? – Sheehan Alam Jun 10 '12 at 08:46
  • possible duplicate of [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar), [NSMutableArray addObject: not affecting count](http://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count), [NSMutableArray addObject: not working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working) – jscs Jun 10 '12 at 18:21

1 Answers1

2

@property statements create the pointers for you, but not the actual content. Since I don't see it in your code: Do you alloc init your NSMutableArray *workspaces somewhere?

EDIT: Probably the best way to initialise your workspaces array would be in a getter method in the class where you define the @property. Lazy initialisation is preferrable. Best amend your @synthesize statement to read something like @synthesize workspaces = i_workspaces;, then you can write your getter like this:

- (NSMutableArray *)workspaces {
    if (i_workspaces == nil) {
        NSMutableArray *i_workspaces = [[NSMutableArray alloc] initWithCapacity:3];
    }
    return i_workspaces;
}

The capacity statement just helps the compiler optimise, it doesn't have to be exact.

Stefan
  • 1,347
  • 2
  • 16
  • 38
  • you mean my NSMutableArray correct? I don't alloc init it anywhere. Should I do it in viewDidLoad? – Sheehan Alam Jun 10 '12 at 08:39
  • Probably the most solid solution would be to write a lazy-initialisation getter in `FLWorkspaceViewController`. I'll amend my answer in a second... – Stefan Jun 10 '12 at 08:40