尝试将CLLocation对象传回iOS应用扩展时,字典变为nil [英] Dictionary becomes nil when trying to pass back CLLocation object to iOS app extension

查看:99
本文介绍了尝试将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).

为此,我使用了扩展代码中WKInterfaceController类中推荐的openParentApplication(userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!, NSError!) -> Void)!) -> Bool函数.我已经通过应用程序委托的application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!)函数在iOS应用程序中成功接收了请求.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆