使用@property ivars的最佳做法 [英] Best practice on using @property ivars

查看:68
本文介绍了使用@property ivars的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请参见我的示例:

@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?

谢谢...

推荐答案

Apple对此主题的指导包含在恰当命名的

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

这篇关于使用@property ivars的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆