encodeWithCoder:发送到实例的无法识别的选择器 [英] encodeWithCoder: unrecognized selector sent to instance

查看:155
本文介绍了encodeWithCoder:发送到实例的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSCoding协议来读取和写入数据到plist。当我尝试编写[GolfHoles]时,我得到一个例外,它是NSObject的子类。我已经阅读了几个不同方法的帖子,但都没有帮助。

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)

    }
}

}

如何编写和读取数组?

推荐答案

rmaddy 是对的。您需要保存所有要保存的类,以符合NSCoding。所以这里有一个GolfHole类的简单示例以及如何序列化GolfCourse对象。

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")
  }
}

这篇关于encodeWithCoder:发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆