【发布时间】:2015-06-10 02:19:45
【问题描述】:
调用 WKInterfaceDevice addCachedImage(_:name:) 将图像从我的 iPhone 应用程序发送到 Apple Watch(扩展程序可以告诉图像视图显示它)崩溃。例外是:
2015-06-09 20:47:57.079 TimeInterval[20195:5186462] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[3]'
各种 Google 和 StackOverflow 搜索表明,这与使用快捷方式创建不允许传入 nil 的 NSDictionary 有关。但是,我的代码根本没有制作字典。此外,当调试器中断(异常断点)时,我验证我传递的 UIImage 和 NSString 名称都绝对不是 nil。
还有其他人看过吗?知道为什么会发生或如何解决吗?有没有人真正成功地使用了 addCachedImage? (考虑到 AppleWatch 有多新,谁知道呢!)
我的代码块,以防万一:
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
if let info = userInfo {
NSLog("watch kit request: %@", info)
if let element = info["element"] as? String {
//Request to render an element
if element == "timer" {
let timerView = NPProgressLabel(size: CGSizeMake(48, 48))
timerView.text = info["value"] as! String
timerView.progressPercent = (info["progress"] as! NSNumber).floatValue
timerView.render()
let device = WKInterfaceDevice.currentDevice()
var success = false
if let image = timerView.currentImage {
success = device.addCachedImage(image, name: timerView.currentImageName()) // <------- crashing here ----------
} else {
NSLog("no image");
}
if !success {
NSLog("failed")
} else {
NSLog("addCachedImage success")
}
reply(["imageName": timerView.currentImageName()])
} else {
reply(["error": "Unknown element"])
}
return
}
}
reply(["error": "Bad request"])
}
【问题讨论】: