Re: Problem instantiating an array
Re: Problem instantiating an array
- Subject: Re: Problem instantiating an array
- From: Conrad Shultz <email@hidden>
- Date: Mon, 06 Dec 2010 22:44:49 -0800
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Dennis Birch wrote:
> I'm new to Cocoa programming and brand new to this list, so I hope it's appropriate to ask this question here.
Welcome!
> // Initialization code here.
> TileColumn *allColumns[boardDimension - 1];
> for (int i = 0; i < boardDimension; i++)
> {
> TileColumn *column = [[TileColumn alloc] init];
> allColumns[i] = column;
> }
>
> gameColumns = [NSArray arrayWithObjects:allColumns count: boardDimension];
I try, for simplicity's sake, to avoid intermingling C types and Cocoa
objects. I would probably rewrite your loop to use an NSMutableArray as
follows - *written in email, untested*:
NSMutableArray *allColumns = [[NSMutableArray alloc]
initWithCapacity:boardDimension];
for (NSUInteger i = 0; i < boardDimension; i++) {
// NSMutableArray retains objects added to it, so autorelease the
following unless you need it for something else
TileColumn *column = [[[TileColumn alloc] init] autorelease];
[allColumns addObject:column];
}
// Now you have an NSMutableArray containing all the columns, owned by you.
// If you really want an NSArray, perhaps for memory reasons, you can
follow up with:
NSArray *tableColumns = [NSArray arrayWithArray:allColumns];
[allColumns release];
// Now you have released allColumns and have an autoreleased
tableColumns - retain it if you need it to persist. You can of course
change the initializer calls to return either owned or autoreleased
objects depending on what suits you best - just follow the memory
management rules!
- --
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkz91+EACgkQaOlrz5+0JdV10wCfb1BcYDAzSvJIWOjyIdvfnoAV
7EIAnjoAHbB5p9EEyYcXTj3CpiiBTcmU
=Wa9I
-----END PGP SIGNATURE-----
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden