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: Lee Ann Rucker <email@hidden>
- Date: Wed, 18 Jul 2012 00:09:20 -0700 (PDT)
In init, and only when they need it. They're all initialized to nil, which is a perfectly reasonable value for an instvar to have; there's very rarely a reason to do anything like
> name = [NSString string];
because sending a method to nil is perfectly safe, unlike C++.
> foodLists = [NSMutableArray array];
is necessary, as you've seen, but not for the reasons you think.
> it crashes.
Somewhere else, I assume? It's not going to crash on that line; is something expecting to get "foo" back out of your object? That's not going to happen because there's nothing hanging on to it; "[m1.foodLists addObject:foo" is a no-op when foodLists is nil.
Declaring that foodLists is an NSMutableArray doesn't mean that there will be an NSMutableArray there, just that you're telling the compiler you're going to use it to store one.
----- Original Message -----
From: "fly2never" <email@hidden>
To: email@hidden
Sent: Sunday, June 24, 2012 10:48:36 PM
Subject: do you init your instance variables in init method or outside the class?
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
_______________________________________________
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