• 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
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]

do you init your instance variables in init method or outside the class?


  • Subject: do you init your instance variables in init method or outside the class?
  • From: fly2never <email@hidden>
  • Date: Mon, 25 Jun 2012 13:48:36 +0800

I have a class Monkey like this:
@interface Monkey : NSObject
{
  NSString *name;
  NSMutableArray *foodLists;
  NSString *id;
  int age;
}

@property blah blah.........

my init method looks like this:
Method 1
- (id)init
{
  if ((self = [super init])) {
    name = [NSString string];
    foodLists = [NSMutableArray array];
    id = [NSString string];
  }
  return nil;
}

So when I call Monkey *m1 = [[Monkey alloc] init];
I can use it directly. [m1.foodLists addObject:foo]; and everything goes
fine.

But if my init method like this :
Method 2
- (id)init
{
  if ((self = [super init])) {
    // do nothing, leave instance variables uninitialized.
  }
  return nil;
}

Then I call Monkey *m1 = [[Monkey alloc] init];
and I call [m1.foodLists addObject:foo]; , it crashes.
I must use m1.foodLists = [NSMutableArray array]; and [m1.foodLists
addObject:foo];

====================================
Besides this two way to init instance variables, which one is the best
practice?
Method 1 ensure that all instance variables(properties) are initialized and
no crash happened, but I think it's redundantly and inconvenient.
Because like id and name. [NSString string] means nothing, I often assign a
new value from outside, but init NSMutableArray looks necessary.
Method 2 make no guarantee. If you need instance variables(properties),
init it first before you use.
what's your choice? Comments welcome : )
_______________________________________________

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

  • Follow-Ups:
    • Re: do you init your instance variables in init method or outside the class?
      • From: Lee Ann Rucker <email@hidden>
    • Re: do you init your instance variables in init method or outside the class?
      • From: "Richard Altenburg (Brainchild)" <email@hidden>
    • Re: do you init your instance variables in init method or outside the class?
      • From: Graham Cox <email@hidden>
    • Re: do you init your instance variables in init method or outside the class?
      • From: Charles Srstka <email@hidden>
  • Prev by Date: Drag and drop issue - CoreDragGetMouseLocation
  • Next by Date: Re: Trap mouse and keyboard events
  • Previous by thread: Drag and drop issue - CoreDragGetMouseLocation
  • Next by thread: Re: do you init your instance variables in init method or outside the class?
  • Index(es):
    • Date
    • Thread