Must call a designated initializer of the superclass 'Day' 错误

Must call a designated initializer of the superclass 'Day' error

在我的应用程序中,我尝试使用数据持久性来保存数据,但出现此错误: 必须调用 superclass'Day' 错误的指定初始化器。

这是我的代码:

class Day: NSObject, NSCoding { // NEMA NSCODING I NSOBJECT
var dayName: String
var subjects: [Subject]?

init(dayName: String) {
    self.dayName = dayName
}

struct PropertyKey {
    static var dayName = "dayName"
    static var subjects = "subjects"
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(dayName, forKey: PropertyKey.dayName)
    aCoder.encode(subjects, forKey: PropertyKey.subjects)
}

// Archiving paths
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("workingDays")//-namesto meals

required convenience init?(coder aDecoder: NSCoder) {
    guard let dayName = aDecoder.decodeObject(forKey: PropertyKey.dayName) as? String else {
        os_log("Unable to decode the dayName for a Day object.", log: OSLog.default, type: .debug)
        return nil
    }
    let subjects = aDecoder.decodeObject(forKey: PropertyKey.subjects) as? [Subject]
    self.init(dayName: dayName)
}

}

这是另一个 class:

class Subject: Day {
var subjectName: String
var startsAt: String?
init(dayName: String,subjectName: String) {
    self.subjectName = subjectName
    super.init(dayName: dayName)
}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder) // here I get the error
}

}

我是否要通过仅在第 class 天实施数据持久化来以这种方式保存数据,为什么会出现该错误? 我是初学者,我是根据这个苹果文档来做的 -

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=12&cad=rja&uact=8&ved=0ahUKEwjWoIHk4uvXAhUHaFAKHbQFCHcQFghbMAs&url=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fcontent%2Freferencelibrary%2FGettingStarted%2FDevelopiOSAppsSwift%2FPersistData.html&usg=AOvVaw24H5Uo4RyRY7NYIlztlgCj

如有任何帮助,我们将不胜感激。

根据必需初始化器的定义-:

Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer:

You do not write the override modifier when overriding a required designated initializer:

在你的例子中,你创建了超类 requiredconvenience initialiser,所以子类现在必须实现它 -:

代码更正-:

class Subject: Day {
    var subjectName: String
    var startsAt: String?
    init(dayName: String,subjectName: String) {
        self.subjectName = subjectName
        super.init(dayName: dayName)
    }

    required convenience init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}

问题是在Dayinit?(coder中调用self.init引起的。该调用使初始值设定项 convenient 而子类无法满足调用指定初始值设定项的要求。

解决方法是直接初始化属性

required init?(coder aDecoder: NSCoder) {
    self.dayName = aDecoder.decodeObject(forKey: PropertyKey.dayName) as! String
    self.subjects = aDecoder.decodeObject(forKey: PropertyKey.subjects) as? [Subject]
}

顺便说一句:您正在将 always dayName 编码为 non-optional 字符串,因此它永远不会 nil 解码时。 guard没用。

在子类中,您可能需要将代码添加到 en-/decode 子类的属性并调用 super 以同时考虑超类的属性。

class Subject: Day {
    var subjectName: String
    var startsAt: String?

    init(dayName: String, subjectName: String) {
        self.subjectName = subjectName
        super.init(dayName: dayName)
    }

    override func encode(with aCoder: NSCoder) {
        super.encode(with: aCoder)
        aCoder.encode(subjectName, forKey: "subjectName")
        aCoder.encode(startsAt, forKey: "startsAt")
    }

    required init?(coder aDecoder: NSCoder) {
        subjectName = aDecoder.decodeObject(forKey: "subjectName") as! String
        startsAt = aDecoder.decodeObject(forKey: "startsAt") as? String
        super.init(coder: aDecoder)
    }
}

关于在其超类中使用子类作为 属性 的意义的问题是另一回事