gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:11:28 24 4
gpt4 key购买 nike

在我的应用程序中,我尝试使用数据持久化来保存数据,但出现此错误:必须调用父类(super class)“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 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
}

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

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

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

最佳答案

问题是在 init?(coder of Day 中调用 self.init 引起的。那个调用使得初始化器 很方便 并且子类无法满足调用指定初始化程序的要求。

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

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 没用。

在子类中,您可能需要添加代码来编码/解码子类的属性,并调用 super 来同时考虑父类(super class)的属性。

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)
}
}

关于将子类用作其父类(super class)中的属性的意义的问题是另一回事 😉

关于swift - Must call a designated initializer of the superclass 'Day' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47610151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com