1
我试图使用NSCoding协议读取和写入数据到plist。当我尝试编写NSObject的子类[GolfHoles]时,我得到一个异常。我用不同的方法阅读过几篇文章,但都没有帮助。encodeWithCoder:无法识别的选择器发送到实例
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)
}
}
}
我怎样写和读的阵列?
无法决定是否要关闭投票的欺骗或不提供异常说明。无论哪种方式,它是接近。 –
可能因为没有阅读ios标签wiki而关闭投票:-o ...它说“请按照文章[我的应用程序崩溃。现在什么?](http://www.raywenderlich.com/10209/my-app在发布与应用程序崩溃相关的任何问题之前,Ray Wenderlich已经发表了一篇文章,其中解释了如何正确调试一个iOS应用程序,当你没有一个应用程序时,提出与崩溃相关的问题是毫无意义的。 *正确*回溯和异常消息。“ – Michael
您的'GolfHole'类是否符合'NSCoding'协议?它需要像你的“GolfCourse”类一样实施两种协议方法。 – rmaddy