title: swift: nscoding decodeObject with nil all the time
body: <p>I got the following codes on writing an object named Packet and send to the other side through Multipeer connectivity. However, I got the following error whenever it try to decode the encoded object.</p>
  
  <pre><code>  class Packet : NSObject, NSCoding {
  
    var tmp1: Double = 0
    var tmp2: Double = 0
  
    struct PropertyKey {
      static let tmp1Key = "tmp1Key"
      static let tmp2Key = "tmp2Key"
    }
  
  
    init(tmp1: Double, tmp2: Double) {
      self.tmp1 = tmp1
      self.tmp2 = tmp2
      super.init()
    }
  
    deinit {
    }
  
    required convenience init(coder aDecoder: NSCoder) {
      debugPrint("initcoder")
      let tmp1 = aDecoder.decodeObject(forKey: PropertyKey.tmp1Key) as! Double // crash here
      let tmp2 = aDecoder.decodeObject(forKey: PropertyKey.tmp2Key) as! Double
      self.init(tmp1: tmp1, tmp2: tmp2)
    }
  
    public func encode(with aCoder: NSCoder) {
      debugPrint("encodeCoder")
      aCoder.encode(tmp1, forKey: PropertyKey.tmp1Key)
      aCoder.encode(tmp2, forKey: PropertyKey.tmp2Key)
    }
  }
  </code></pre>
  
  <p>The error I got is 
  ---- Print out from me  ---- "initcoder"
  fatal error: unexpectedly found nil while unwrapping an Optional value
  2016-09-30 13:32:55.901189 Connection[323:33022] fatal error: unexpectedly found nil while unwrapping an Optional value</p>
  
  <p>But when I construct the object, all the values are set.
  I contracted a Packet object with no problem.</p>
  
  <p>----  Additional information ------
  I used the following codes for encode and decode the data when send to the other side through multiplier connectivity.</p>
  
  <pre><code> func dataForPacket(packet: Packet) -&gt; Data {
      let data = NSMutableData()
      let archiver = NSKeyedArchiver(forWritingWith: data)
      archiver.encode(packet, forKey: "Packet")
      archiver.finishEncoding()
      debugPrint("dataForPacket \(data) \(packet)")
      return data as Data
    }
  
    func packetForData(_ data: Data) -&gt; Packet {
      debugPrint("packetForData")
      let unarchiver = NSKeyedUnarchiver(forReadingWith: data)
  
      let packet: Packet = unarchiver.decodeObject(forKey: "Packet") as! Packet 
       // crash here (as this will call the init of the Packet class)
      debugPrint("packetForData \(packet)")
      return packet
    }
  </code></pre>
  
  <p>I wonder what can cause the error. Thanks.</p>