我一直在搜索此错误并阅读各种stackoverflow,Apple文档和博客答案,但我仍然遇到问题。我有一个符合NSData和NSCoding的类,它将存储6个属性中的3个。当在所需的方便init中调用self.init时,我在调用中得到一个错误“额外参数'groomedStatus',”即使self.init签名与init完全匹配(我复制并粘贴)。这是相关的代码:
class Trail: NSObject, NSCoding {
var name: String
var difficulty: Difficulty
var haveSkied: Bool
var season: Season = .winter
var open: String?
var groomedStatus: String?
init(name: String, difficulty: Difficulty, haveSkied: Bool, season: Season, open: String?, groomedStatus: String?) {
self.name = name
self.difficulty = difficulty
self.haveSkied = haveSkied
self.season = season
self.open = open
self.groomedStatus = groomedStatus
}
required convenience init(coder aDecoder: NSCoder) {
let haveSkied = aDecoder.decodeObjectForKey("haveSkied") as! Bool
let open = aDecoder.decodeObjectForKey("open") as? String
let groomedStatus = aDecoder.decodeObjectForKey("groomedStatus") as? String
self.init(name: String, difficulty: Difficulty, haveSkied: Bool, season: Season, open: String?, groomedStatus: String?) {
self.name = name
self.difficulty = difficulty
self.haveSkied = haveSkied
self.season = season
self.open = open
self.groomedStatus = groomedStatus
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeBool(haveSkied, forKey: "haveSkied")
aCoder.encodeObject(open, forKey: "open")
aCoder.encodeObject(groomedStatus, forKey: "groomedStatus")
}
}
答案 0 :(得分:1)
问题源于您致电self.init()
的方式。您在代码中执行此操作的方式本质上是尝试重新定义它,这对编译器没有意义。这就是你应该在便利初始化程序中调用self.init()
的方法。
self.init(name: "yourActualNameString", difficulty: Difficulty(), haveSkied: true, season: .winter, open: "yourActualOpenString", groomedStatus: "yourActualGroomedStatusString")