I'm attempting use NSCoding protocol to read and write data to plist. I get an exception when I try to write the [GolfHoles] which is a subclass of NSObject. I've read several posts with different approaches, but none have helped.
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)
}
}
}
How do I write and read the array?