패킷 (Packet)이라는 객체를 작성할 때 다음 코드를 얻었고 Multipeer 연결을 통해 상대방에게 보냅니다. 그러나 인코딩 된 개체를 디코딩하려고 할 때마다 다음 오류가 발생합니다.swift : nscoding decodeObject를 항상 nil로 설정
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)
}
}
내가 가진 오류는 ---- 내에서 밖으로 인쇄 치명적인 오류 "initcoder"---- 입니다 : 2016년 9월 30일 13시 32분하는 옵션 값을 풀기 동안 예기치 않게 전무을 발견 : 55.901189 Connection [323 : 33022] 치명적인 오류 : 선택 값을 언 래핑하는 동안 예기치 않게 nil이 발견되었습니다.
하지만 개체를 구성 할 때 모든 값이 설정됩니다. 나는 Packet 객체를 아무런 문제없이 계약했다.
---- 추가 정보 ------ 멀티 플라이어 연결을 통해 상대방에게 보낼 때 데이터 인코딩 및 디코딩에 다음 코드를 사용했습니다.
func dataForPacket(packet: Packet) -> 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) -> 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
}
나는 무엇이 오류가 발생할 수 있습니다. 감사.
당신은 키 –
에 대한 decodeDouble를 사용할 필요가'하자 tmp1 = aDecoder.decodeDouble (forKey : PropertyKey.tmp1Key)' –
위의 의견은 당신이 정수에 대한 특정 디코드를 사용할 필요가 언급 한 것처럼 , float, bool, double 등. – mn1