【问题标题】:2 ways to Objective C custom init? [duplicate]Objective C自定义初始化的2种方法? [复制]
【发布时间】:2015-01-18 15:38:26
【问题描述】:

我正在学习如何在 Objective-C 中创建自定义 INIT。似乎有两种方法可以做到这一点。这两种创建自定义初始化的方法有什么区别吗?

//First way of creating a custom init uses instancetype 

-(instancetype) init
{
   self = [super init];
   if (self) {
    //initialization code here
   }
   return self:

}

//Second way of creating a custom init uses ID

- (id)init {
    self = [super init];
    if (self) {
        //initialization code here
    }
    return self;
}

【问题讨论】:

    标签: objective-c initialization instancetype


    【解决方案1】:

    一直使用instancetype 是安全的,但请记住,实现(.m)(通常)对其他类不可见。因此,在许多情况下,在 .h 中声明 instancetype 和在 .m 中声明 id 会产生相同的编译时安全性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多