encodeWithCoder: unrecognized selector sent to instance

user2966301 picture user2966301 · Sep 12, 2015 · Viewed 10.4k times · Source

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?

Answer

Price Ringo picture Price Ringo · Sep 12, 2015

rmaddy is right. You need to have all of your classes that will be saved to also conform to NSCoding. So here is a trivial example of a GolfHole class and how to serialize the GolfCourse object.

class GolfHole: NSObject, NSCoding {
  let number: Int
  let par: Int

  init(number: Int, par: Int) {
    self.number = number
    self.par = par
  }

  convenience required init?(coder aDecoder: NSCoder) {
    guard
      let number = aDecoder.decodeObjectForKey("number") as? Int,
      let par    = aDecoder.decodeObjectForKey("par")    as? Int
    else {
      return nil
    }
    self.init(number: number, par: par)
  }

  func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(number, forKey: "number")
    aCoder.encodeObject(par,    forKey: "par")
  }
}


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

  init(name: String, location: String, holes: [GolfHole]) {
    self.name = name
    self.location = location
    self.holes = holes
  }

  convenience required init?(coder aDecoder: NSCoder) {
    guard
      let name     = aDecoder.decodeObjectForKey("name")     as? String,
      let location = aDecoder.decodeObjectForKey("location") as? String,
      let holes    = aDecoder.decodeObjectForKey("holes")    as? [GolfHole]
    else {
      return nil
    }
    self.init(name: name, location: location, holes: holes)
  }

  func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(name,     forKey: "name")
    aCoder.encodeObject(location, forKey: "location")
    aCoder.encodeObject(holes,    forKey: "holes")
  }
}

enter image description here