Initially i declared a 2D array in this way:
subUrb = [[NSArray alloc] initWithObjects:
[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil] ,
[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Kuala Lumpur SubUrb1", @"Kuala Lumpur SubUrb2", nil],
[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Jakarta SubUrb1",nil],
nil];
But when i try to ‘analyze’ the warnings of the project, i got three same type of issues in this chunk of code – “potential leak of an object allocated on line xxx”
I noticed that to get rid of it, i have to write sth like this:
subUrb =
[[NSArray alloc] initWithObjects:
[[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil] autorelease],
[[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Kuala Lumpur SubUrb1", @"Kuala Lumpur SubUrb2", nil] autorelease],
[[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Jakarta SubUrb1",nil] autorelease],
nil];
Then i will not get any analyze warnings. But i dun like this…it is not logical. This 2D array should never be released in my controller actually, the entire 2D array should be retained for the entire life time of the controller for a PickerView.
How should I declare the 2D array more elegantly?
You get the ownership if you allocate an array with alloc init and so you have to release it as you do in your second code.
You can also use:
[[NSArray alloc] initWithObjects:[NSArray arayWithObjects:first,second, nil], nil];