Objective-C Properties with or without instance va

2019-01-25 23:20发布

Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0

I'm using properties only for ivars that don't require any real validation when getting or setting a value for a class that I'm authoring.

Ex: 
@interface ClassB : NSObject
{
    // No ivars
}

@property (nonatomic, retain) ClassA *obj;
@property (nonatomic, retain) NSString *str;

Whenever I call these methods within the class I'm always using self.obj/self.str or [self obj]/[self str]. This way in order to set the state of the object you are forced to go through the property so that the references are more carefully managed by the compiler produced getters and setters. With that said, are there any problems that I can run into creating my classes this way? Also, with no ivar present, is any "release" needed in an overwritten "dealloc" method in the @implementation file?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-25 23:34

If you're using the @synthesize directive to create the property accessors, the compiler will also create any ivars that you leave out of the class declaration. You do still need to release your ivars. It used to be that you couldn't access synthesized ivars directly and so had to use the property setter in your -dealloc method like this:

- (void)dealloc
{
    self.obj = nil;
    self.str = nil;
    [super dealloc];
}

These days, though, the compiler will let you access the synthesized ivar directly, so you can release it instead of calling the setter (which is a good idea in -dealloc, and a bad idea everywhere else):

- (void)dealloc
{
    [obj release];
    [str release];
    [super dealloc];
}

This will change when you eventually convert your code for ARC (automatic reference counting) because the compiler will take care of releasing your ivars in -dealloc for you. Most of the time, this means that your -dealloc implementation can just go away.

查看更多
3楼-- · 2019-01-25 23:36

It's not necessary to have your IVars declared explicitly. I'd even recommend omitting them because you should ALWAYS access IVars via accessors (as you pointed out what you already do.)

You will not run in any problems, except that the debugger doesn't catch the IVars directly anymore (they won't display nicely next to your console.) - But that's not really an issue.

In your dealloc method you will not need to release them anymore. What you will need to do is nil them.

self.obj = nil;

--

Here's a brief overview of what the property attributes actually mean: (That's to show you that a release happens when you nil a variable in the dealloc)

assign

@property (assign) NSString *name;

name = newValue;

retain

@property (retain) NSString *name;

if (name != newValue) {
   [name release];
   name = [newValue retain];
}

copy

@property (copy) NSString *name;

if (name != newValue) {
   [name release];
   name = [newValue copy];
}
查看更多
乱世女痞
4楼-- · 2019-01-25 23:47

The compiler is automagically creating the ivar for you, even though it's not explicitly defined in the ivar list. It's got the same name as in the property list. The compiler now just doesn't require the duplicate definition line.

If you have properties (ivars) that need to be released, do it.

查看更多
登录 后发表回答