我试图将我的 swift 对象从 iOS 应用程序传递到 Watch。但是,我发现它适用于 NSString 等基本类型,但我的自定义对象类型。
我的自定义对象能够转换为 NSData
我已经让我的对象实现了 NSObject 和 NSCoding,效果很好。我可以毫无问题地执行以下操作:
let encodedChordProgression = NSKeyedArchiver.archivedDataWithRootObject(chordProgressions[1])
let decodedChordProgression = NSKeyedUnarchiver.unarchiveObjectWithData(encodedChordProgression) as! ChordProgression
NSLog("get decodedChordProgression = \(decodedChordProgression.description)")
WatchConnectivity 代码适用于 NSString
在 iPhone 中:
try WatchSessionManager.sharedManager.updateApplicationContext(["data": "mystringishere"])
带手表:
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.dataSourceChangedDelegates.forEach { $0.dataSourceDidUpdate(applicationContext["data"] as! NSString)}
}
作品。
我的 WatchConnectivity 自定义对象失败
但是,当我将对象切换到自己的对象时,由于没有调用dataSourceChangedDelegates
回调函数而失败。那是:
在 iPhone 中:
let encodedChordProgression = NSKeyedArchiver.archivedDataWithRootObject(chordProgressions[1])
try WatchSessionManager.sharedManager.updateApplicationContext(["data": encodedChordProgression])
带手表:
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.dataSourceChangedDelegates.forEach { $0.dataSourceDidUpdate(applicationContext["data"] as! NSData)}
}
和
func dataSourceDidUpdate(encodedChordProgression: NSData) {
let chordProgression = NSKeyedUnarchiver.unarchiveObjectWithData(encodedChordProgression) as! ChordProgression
NSLog("get something here: \(chordProgression.description)")
}
我尝试过的和我的问题
我试图阅读system.log
iPhone 应用程序和 Watch 应用程序,但我找不到任何线索,这是我现在遇到的最大问题。
完整代码是:here (checkout 7f2a72c6004f6580e2a38a2d7fd0ed2cef8a2b2e
)