Re: do you init your instance variables in init method or outside the class?
Re: do you init your instance variables in init method or outside the class?
- Subject: Re: do you init your instance variables in init method or outside the class?
- From: Charles Srstka <email@hidden>
- Date: Wed, 18 Jul 2012 00:08:06 -0500
On Jun 25, 2012, at 12:48 AM, fly2never wrote:
> Then I call Monkey *m1 = [[Monkey alloc] init];
> and I call [m1.foodLists addObject:foo]; , it crashes.
That won’t crash, because all instance variables are automatically initialized to nil. Sending the addObject: message to a nil object won’t crash — but neither will it do what you want. Instead, it won’t do anything, and your ‘foo’ object won’t get added — probably not what you want.
Also, I wouldn’t give the user direct access to your internal NSMutableArray and let them monkey with it directly. Instead, implement a to-many property backed by the NSMutableArray, with which the users of the class can interact instead of directly with the array.
Charles
_______________________________________________
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