Best practice on using @property ivars

半世苍凉 提交于 2020-01-04 20:21:32

问题


Could someone share some knowledge on whats best practice / code convention on using @property iVars in init methods or designated initializers?

please see my example:

@interface MyClass ()
@property(nonatomic,strong) nsstring *tempString;
@property(nonatomic,strong) NSMutableArray *arrItems;
@end

@implementation ViewController

- (id)init
{
    if (self = [super init]) {

        //Is this best practice / correct
        _tempString = @"";
        _arrItems = [[NSMutableArray alloc] initWithCapacity:0];
        ...
        ...

        //Or this
        self.tempString = @"";
        self.arrItems = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
}

@end

Any advice on why one or the other should be used?

Thanks...


回答1:


Apple's guidance on this topic is included in the aptly named section Don’t Use Accessor Methods in Initializer Methods and dealloc.




回答2:


Read this thread: Why shouldn't I use Objective C 2.0 accessors in init/dealloc?

In other words if you are not goiung to use KVO you can use second approach:

//Or this
        self.tempString = @"";
        self.arrItems = [[NSMutableArray alloc] initWithCapacity:0];

But be care full with alloc-init, don't forget about autorelease.




回答3:


It's typically better to use property notation when you define it, partly(mostly?) for the reason Jeremy mentioned.

Debugging a particular variable is a whole lot easier when you can set a breakpoint in method setter override and have it apply to ALL code paths that modify the variable.

Another reason is to keep a consistent memory management model, although it is less important since you are using ARC. If you weren't however, and strong was retain, then you would make sure that the object you are setting to the property is autoreleased everywhere you set the property, and not have to deal with releasing the current value if you are directly setting the variable.

Consistency is important for maintenance/readability and debugging, no matter what practices you use.




回答4:


I prefer the lazy instantiation method for properties.

After you @synthesize you can override your getter to lazily instantiate your property

For Example:

-(NSString *)tempString {

    if(!tempString) {
        _tempString = @"";
    }
    return _tempString;
}

and

-(NSMutableArray *)arrItems {

    if(!_arrItems) {
        _arrItems = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _arrItems;
}

If you do want to set your property in the init method, use dot notation self.myProperty so that it uses the defined setter for the property and not the private class method directly.




回答5:


According to Apple, you should not use accessors in init... or dealloc methods:

You should always access the instance variables directly from within an initialization method because at the time a property is set, the rest of the object may not yet be completely initialized. Even if you don’t provide custom accessor methods or know of any side effects from within your own class, a future subclass may very well override the behavior.

Taken from this doc: Encapsulating Data.



来源:https://stackoverflow.com/questions/12752946/best-practice-on-using-property-ivars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!