本文介绍了尝试将 CLLocation 对象传递回 iOS 应用程序扩展时,字典变为 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将存储的 CLLocation 对象从我的 iOS 应用程序传递回扩展程序(在这种情况下:Apple Watch 扩展程序).

I'm trying to pass a stored CLLocation object from my iOS app back to an extension (in that case: an Apple Watch extension).

为此,我正在使用推荐的 openParentApplication(userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!, NSError!) -> Void)!) ->来自扩展代码中 WKInterfaceController 类的 Bool 函数.我通过应用程序委托的 application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, 回复: (([NSObject : AnyObject]!) -> Void)!) 函数.

For that, I'm using the recommended openParentApplication(userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!, NSError!) -> Void)!) -> Bool function from the WKInterfaceController class in the extension code. I'm successfully receiving the request in my iOS app through the application delegate's application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) function.

现在,我正在尝试使用以下代码将数据传回应用扩展程序:

Now, I'm trying to pass data back to the app extension using this code:

    var results = [NSObject: AnyObject]()
    if let loc = getLastKnownLocation() {
        results["hasLoc"] = "yes"
        results["locStr"] = "\(loc)"
        results["results"] = loc // at this point, loc is actually a valid CLLocation object
    }
    reply(results)

所有这些代码都在 iOS 应用的上下文中执行,并且一切正常.然而,当 reply 函数在扩展的上下文中被调用时,字典最终变成 nil.

All this code is executed in the iOS app's context and everything is working fine. However, when the reply function gets called in the extension's context, the dictionary ultimately becomes nil.

    InterfaceController.openParentApplication(parentValues, reply: {(values, error) -> Void in
        NSLog("\(values) (err: \(error))") // echoed back to the console: "nil (err: nil)"
    })

我意识到有问题的线路是这条线:

I realised that the faulty line is this one:

                results["results"] = loc

当这一行被注释时,扩展成功地获得了一个字典,其中填充了前两个项目作为字符串("hasLoc""locStr"):

When this line is commented, the extension successfully gets a dictionary filled with the two first items as strings ("hasLoc" and "locStr"):

[locStr: Optional(<+37.33756323,-122.04115699> +/- 10.00m (speed 5.00 mps / course 272.00) @ 21/12/2014 19:20:05 heure normale d’Europe centrale), hasLoc: yes] (err: nil)

所以似乎在我的字典中插入一个 CLLocation 对象会使它全部失败并变为 nil,但我不明白为什么.我做错了什么?

So it seems like inserting a CLLocation object in my dictionary makes it all fail and become nil, but I can't understand why. What am I doing wrong?

感谢您的帮助.

根据@matt 的请求,这里是 getLastKnownLocation() 的代码:

per @matt's request, here's the code for getLastKnownLocation():

class func getLastKnownLocation() -> CLLocation? {
    return NSKeyedUnarchiver.unarchiveObjectWithData(NSUserDefaults(suiteName: AppHelper.appGroupName)!.dataForKey("UserLastLocation")!) as CLLocation?
}

推荐答案

我在文档中读到:字典的内容必须可序列化为属性列表文件."但是 CLLocation 不能以这种方式序列化.据推测,您必须(再次)将其包装在 NSData 中,然后再将其粘贴到字典中以通过障碍.

I read in the docs: "The contents of the dictionary must be serializable to a property list file." But a CLLocation is not serializable in this way. Presumably you must wrap it up (again) in an NSData before you stick it into the dictionary to be passed across the barrier.

这篇关于尝试将 CLLocation 对象传递回 iOS 应用程序扩展时,字典变为 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:28