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