使用 @property ivars 的最佳实践

2024-01-01

有人可以分享一些关于在 init 方法或指定初始化程序中使用 @property iVars 的最佳实践/代码约定的知识吗?

请看我的例子:

@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

关于为什么应该使用其中之一有什么建议吗?

谢谢...


Apple 关于此主题的指南包含在适当命名的部分中不要在初始化方法和 dealloc 中使用访问器方法 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW6.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 @property ivars 的最佳实践 的相关文章

随机推荐