gpt4 book ai didi

swift - 尝试将 CLLocation 对象传回 iOS 应用程序扩展时,字典变为零

转载 作者:搜寻专家 更新时间:2023-10-31 22:08:47 24 4
gpt4 key购买 nike

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

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

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

    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

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

我意识到故障线是这条线:

                results["results"] = loc

当这一行被注释时,扩展成功地获得了一个字典,其中填充了前两个项目作为字符串(“hasLoc”“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,但我不明白为什么。我做错了什么?

感谢您的帮助。

编辑:根据@matt 的要求,这是 getLastKnownLocation() 的代码:

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

最佳答案

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

关于swift - 尝试将 CLLocation 对象传回 iOS 应用程序扩展时,字典变为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27592395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com