编码和解码SKSpriteNode

时间:2018-10-21 23:19:42

标签: swift skspritenode nscoder

我对如何解码SKSpriteNode感到有些困惑。

我有以下SKSpriteNode类:

class Person : SKSpriteNode
{
  var gender : Gender
  var age : Age
  var positionX : CGFloat!
  var positionY : CGFloat!
  let frontTexture : SKTexture
  let backTexture : SKTexture

  required convenience init?(coder aDecoder: NSCoder)
  {

    print("INITIALIZED WITH DECODER????")

    guard let myPerson = aDecoder.decodeObject(forKey: "person") as? Person
        else { return nil }

    self.init(coder: aDecoder)
  }

  func encodeWithCoder(coder: NSCoder)
  {
    coder.encode(self, forKey: "person")
  }


  init(gender: Gender, age: Age, positionX: CGFloat, positionY: CGFloat, sceneSize: CGSize)
  {
    self.gender = gender
    self.age = age

    self.backTexture = SKTexture(imageNamed: "person_back")
    self.frontTexture = SKTexture(imageNamed: "person_front")

    var currentTexture = self.frontTexture

    super.init(texture: currentTexture, color: .clear, size: frontTexture.size())

    // Position of the person in the scene
    self.position = updatePosition(positionX: positionX, positionY: positionY, sceneSize: sceneSize)

  }

在MultipeerConnection子类中,我使用它来发送编码数据(数据似乎已正确存档):

func sendData(dictionaryWithData dictionary: Dictionary<String, Any>, toPeer targetPeers: [MCPeerID])
  {
    let dataToSend = NSKeyedArchiver.archivedData(withRootObject: dictionary)

    do
    {
        try session.send(dataToSend, toPeers: targetPeers, with: MCSessionSendDataMode.reliable)
    }
    catch
    {
        print("ERROR")
    }
  }

数据制作如下(人群是一个数组[Person]):

func makeMessageCrowd() -> Dictionary<String, Any>
  {

    var messageToSend = Dictionary<String, Any>()
    messageToSend["move"] = "start"
    messageToSend["action"] = appDelegate.persons
    return messageToSend
  }

我使用以下方法接收数据:

func session(_ session: MCSession,
             didReceive data: Data,
             fromPeer peerID: MCPeerID)
  {

    let message = NSKeyedUnarchiver.unarchiveObject(with: data) as! Dictionary<String, Any>

    if let moveType = message["move"] as? String
    {
        switch(moveType)
        {
        case "start":
            appDelegate.persons = message["action"] as! [Person]

        default:
            print("default")
        }
    }
  }

现在,它开始使用便捷初始化程序初始化对象Person,然后在此处崩溃:

let message = NSKeyedUnarchiver.unarchiveObject(with: data) as! Dictionary<String, Any>

说它找到了一个零值。但是,数据似乎有15000字节,因此传递得很好。

如果我使用便利初始化程序而不是便捷初始化程序,则使用以下方法:

required init?(coder aDecoder: NSCoder)
  {
    //fatalError("NSCoding not supported")
    self.gender = Gender.male
    self.age = Age.young
    self.frontTexture = SKTexture(imageNamed: "person_front")
    self.backTexture = SKTexture(imageNamed: "person_back")
    self.positionX = 0
    self.positionY = 0

    super.init(coder: aDecoder)
  }

我没有崩溃,但是,对等设备上的数组[Person]是使用如上初始化的默认值创建的,而不是通过传递的。

这是罪魁祸首,因为它返回nil:

guard let myPerson = aDecoder.decodeObject(forKey: "person") as? Person
        else { return nil }

如何正确编码和解码自定义类?

非常感谢!

0 个答案:

没有答案