I am trying to figure out if a step is necessary.
If I create an object which has an NSArray object as a property, and I specify a retain in the accessor methods.
#import <UIKit/UIKit.h>
@interface CellsViewController : UIViewController <UITableViewDataSource, UITabeViewDelegate>
{
NSArray *computers;
}
@property (nonatomic, retain) NSArray *computers;
@end
When I get to the viewDidLoad method I do the following.
- (void)viewDidLoad
{
NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"MacBook", @"Name", @"White", @"Color", nil];
NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys: @"MacBook Pro", @"Name", @"Silver", @"Color", nil];
NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys: @"iMac", @"Name", @"White", @"Color", nil];
NSDictionary *row4 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Mac Mini", @"Name", @"White", @"Color", nil];
NSDictionary *row5 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Mac Pro", @"Name", @"Silver", @"Color", nil];
NSArray *array = [[NSArray alloc] initWithObjects:row1, row2, row3, row4, row5, nil];
self.computers = array;
[row1 release];
[row2 release];
[row3 release];
[row4 release];
[row5 release];
[array release];
}
I am creating a temporary NSArray named array which I use to set the object array.
self.computers = array;
When the array object is set the objects in the array and the array itself are retained which is why I can release all of them in the method. My question has to do with the temporary array. Could I do the following or am I missing something?
self.computers = [[NSArray alloc] initWithObjects:row1, row2, row3, row4, row5, nil];
That way I would not have to create or release the array just the NSDictionary's. I understand the first option looks really clean I am just trying to see if I understand.
Thanks,
Jon