ios - Swift 初始化器之谜

标签 ios swift xcode initializer

几个代码块:

这个有效:

import Foundation

class Class1 {
    init(param1: String?) {
        print("hello")
    }

    convenience init() {
        self.init(param1: nil)
    }
}

class SubClass1: Class1 {

}

let obj = SubClass1()

在上面的代码中,指定的初始化程序被传递给 SubClass1,因为 SubClass1 没有提供自己的指定初始化程序。此外,便利初始化器也被传递下来。因此,这是可行的。

import Foundation

class Class1 {
    init(param1: String?) {
        print("hello")
    }

    convenience init() {
        self.init(param1: nil)
    }
}

class SubClass1: Class1 {
    override init(param1: String?) {
        print("hello 2")
        super.init(param1: nil)
    }
}

let obj = SubClass1()

这也有效,因为 SubClass1 现在已经提供了它自己的指定初始化器。

import Foundation

class Class1 {
    init(param1: String?) {
        print("hello")
    }

    init(param2: String?) {
        print("world")
    }

    convenience init() {
        self.init(param1: nil)
    }
}

class SubClass1: Class1 {
    override init(param1: String?) {
        print("hello 2")
        super.init(param1: nil)
    }
}

let obj = SubClass1()

最后一行给出“错误:调用中参数‘param1’缺少参数”。 在这里,有一个指定的 SubClass1 初始化器,父类的便利初始化器也调用了相同的初始化器,那么为什么会出错?

这个

import Foundation

class Class1 {
    init(param1: String?) {
        print("hello")
    }

    required init(param2: String?) {
        print("world")
    }

    convenience init() {
        self.init(param1: nil)
    }
}

class SubClass1: Class1 {
    override init(param1: String?) {
        print("hello 2")
        super.init(param1: nil)
    }

    required init(param2: String?) {
        print("world")
        super.init(param2: nil)
    }
}

let obj = SubClass1()

这有效:

import Foundation

class Class1 {
    init(param1: String?) {
        print("hello")
    }

    init(param2: String?) {
        print("world")
    }

    convenience init() {
        self.init(param1: nil)
    }
}

class SubClass1: Class1 {
    override init(param1: String?) {
        print("hello 2")
        super.init(param1: nil)
    }

    override init(param2: String?) {
        print("world")
        super.init(param2: nil)
    }
}

let obj = SubClass1()

为什么需要覆盖所有指定的初始化器才能让一个方便的初始化器工作,它只调用指定的初始化器之一?

最佳答案

此行为在 swift guide 中有明确说明.

Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:

Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

Rule 2 If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.

所以基本上,您的子类必须重写/定义父类(super class)的所有指定初始化程序才能继承父类(super class)的便利初始化程序。

关于ios - Swift 初始化器之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46557973/

相关文章:

objective-c - iOS系统声音服务无法调节音量

ios - 如何将 NSNotification.Name 重新定义为枚举?

iphone - 在越狱的全新iOS设备上进行测试-使用Xcode?

iphone - iOS 中的 UI 元素如何从没有线程的循环中更新?

ios - 如何以编程方式将按钮定位到带有边距的表格单元格的最右边缘?

ios - Swift iOS - 如何管理多个类中不同视频的Notification.Name.AVPlayerItemDidPlayToEndTime

ios - 如何检查在 Swift 中是否可以分配?

ios - 从 UITapGestureRecognizer 访问 subview

iphone - 按钮文本未显示且 xcode 崩溃

ios - 为我的应用程序导入 .doc 和 .pdf