【问题标题】:AppleWatch - "attempt to insert nil" when calling WKInterfaceDevice addCachedImageAppleWatch - 调用 WKInterfaceDevice addCachedImage 时“尝试插入 nil”
【发布时间】: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"])
}

【问题讨论】:

    标签: ios swift watchkit


    【解决方案1】:

    我得到的确切错误可能是 Apple 错误,但我认为我的问题的答案是不应从 iPhone 应用程序调用 WKInterfaceDevice 的 addCachedImage,而应从 WatchKit 扩展程序调用。在 iPhone 应用和 WatchKit 扩展之间,我必须使用共享容器来保存然后加载图像,然后扩展可以调用 addCachedImage。

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 2012-08-09
      • 1970-01-01
      • 2011-07-30
      • 2016-05-23
      • 2023-03-29
      • 2012-06-05
      • 1970-01-01
      • 2017-12-16
      相关资源
      最近更新 更多