2015-02-07 54 views
2

從我的iPhone應用程序,我通過將數據發送到我的收藏的應用程序:與iPhone應用程序Watchkit共享數據,NSInvalidUnarchiveOperationException

func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) { 
    if let data = NSKeyedArchiver.archivedDataWithRootObject(Country()) { 
    reply(["data": data]) 
    } 
} 

在我的收藏的應用程序,我試圖讀取數據:

WKInterfaceController.openParentApplication(input, reply: { (replyValues, error) -> Void in 
    if error == nil { 
    if let data = replyValues["data"] as? NSData { 
     if let temp = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? Country { 
     println("done") 
     } 
    } 
    } 
}) 

以下錯誤被拋出:

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Country)' 
+0

[WatchKit和NSUserDefaults的問題NSKeyedUnarchiver]的可能重複(http://stackoverflow.com/questions/28383549/watchkit-nsuserdefaults-and-nskeyedunarchiver-issue) – Jano 2015-02-11 21:00:30

回答

1

的NSKeyedArchiver/NSKeyedUnarchiver只適用於那些類NSCoding兼容。

沒有進一步檢查,似乎你的'國家'類不符合NSCoding協議。

NSHipster並沒有讓你對冗長的解釋感到困惑,它有一個絕妙的頁面,它解釋瞭如何實現NSCoding並使你的對象存檔友好。

我強烈推薦它。

http://nshipster.com/nscoding/

相關問題