我正在尝试将PFUser.currentUser()对象传递给apple watch扩展程序以加载一些数据。我知道我可以用App Groups做到这一点,但这需要激活本地数据存储,我需要使用缓存。所以我在app delegate中实现了这段代码:
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
// retrieved parameters from Apple Watch
//println(userInfo["value1"])
//println(userInfo["value2"])
// pass back values to Apple Watch
var retValues = Dictionary<String,PFUser>()
retValues["retVal1"] = PFUser.currentUser()!
print(PFUser.currentUser()!)
reply(retValues)
}
然而,应用程序崩溃,并说currentUser的值为nil:致命错误:在解包可选值时意外发现nil
func getCurrentUser(){
var dict = ["test" : 4] //your dictionary/request to sent to the iPhone
if !WKInterfaceController.openParentApplication(dict, reply: { (reply,error) -> Void in
println("\(reply)") //your reply data as Dictionary
if let passedCurrentUser: PFUser = reply["currentUser"] as? PFUser{
self.currentUser = passedCurrentUser
self.getTrips()
}
}) {
println("ERROR")
}
}
PFUser.currentUser()显然不是零。打印方法打印(PFUser.currentUser()!)甚至不打印数据。
答案 0 :(得分:0)
使用应用程序组找出一种更简单的方法:
1)在Watch目标和您的应用程序上启用应用程序组
2)进入AppDelegate并插入:
let appGroupID = "group.com.xx"
let defaults = NSUserDefaults(suiteName: appGroupID)
defaults!.setObject(PFUser.currentUser()!.objectId, forKey: "theCurrentUser")
3)在手表扩展名中:
let appGroupID = "group.com.xx"
let defaults = NSUserDefaults(suiteName: appGroupID)
if let theCurrentUser = defaults?.stringForKey("theCurrentUser") {
//do something
}