gpt4 book ai didi

ios - 编码器 : unrecognized selector sent to instance

转载 作者:搜寻专家 更新时间:2023-11-01 05:53:52 24 4
gpt4 key购买 nike

<分区>

我正在尝试使用 NSCoding 协议(protocol)将数据读取和写入 plist。当我尝试编写作为 NSObject 的子类的 [GolfHoles] 时出现异常。我已经阅读了几篇采用不同方法的帖子,但都没有帮助。

class GolfCourse: NSObject, NSCoding {
var name: String = ""
var location: String = ""
var holes: [GolfHole] = [GolfHole]()

init(holes: [GolfHole]) {
self.holes = holes
}

// MARK: NSCoding Protocol
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
aCoder.encodeObject(location, forKey: "location")
aCoder.encodeObject(holes, forKey: "holes") // exception here

}

required init(coder aDecoder: NSCoder) {
super.init()
name = aDecoder.decodeObjectForKey("name") as! String
location = aDecoder.decodeObjectForKey("location") as! String
holes = aDecoder.decodeObjectForKey("holes") as! [GolfHole]

}

override init() {
super.init()
for var i=0; i<18; i++ {
let newHole = GolfHole()
self.holes.append(newHole)

}
}

如何写入和读取数组?

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