• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: do you init your instance variables in init method or outside the class?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >do you init your instance variables in init method or outside the class? (From: fly2never <email@hidden>)

  • Prev by Date: Re: Looking for better solution than this old hack
  • Next by Date: Re: do you init your instance variables in init method or outside the class?
  • Previous by thread: do you init your instance variables in init method or outside the class?
  • Next by thread: Re: do you init your instance variables in init method or outside the class?
  • Index(es):
    • Date
    • Thread