将字典数组从 iPhone 发送到 watchkit 应用程序
Sending array of dictionary from iPhone to watchkit app
代码:
AppDelegate.swift
func application(application:UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!)
{
let entityDescription =
NSEntityDescription.entityForName("Quote",
inManagedObjectContext: managedObjectContext!)
let request = NSFetchRequest()
request.entity = entityDescription
let pred = NSPredicate(format: "(quoteDate = %@)", "2015-03-08")
request.predicate = pred
var error: NSError?
var objects = managedObjectContext?.executeFetchRequest(request,
error: &error)
if let results = objects {
if results.count > 0 {
arrQuotes = NSMutableArray()
for(var i=0;i<results.count;i++){
let match = results[i] as! NSManagedObject
var quote = match.valueForKey("quote") as! NSString
arrQuotes.addObject(quote)
}
var dict = ["test": arrQuotes]
reply(dict)
} else {
}
}
catecontroller.swift
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
arrQuotes = NSMutableArray()
var dict = ["test" : arrQuotes]
if !WKInterfaceController.openParentApplication(dict, reply: { (reply,error) -> Void in
println("\(reply)") //your reply data as Dictionary
self.arrQuotes = dict["test"]!
println("\(self.arrQuotes.count)")
}) {
println("ERROR")
}
我正在做一个示例 watchkit project.What 我正在尝试做的是从 iPhone 端获取数据并将其发送到 watchkit app.I 尝试 return 值作为 watchkit 端的 dictionary.but 数组,我得到的数组计数为 zero.I 不知道我哪里出错了?任何帮助都会提前 appreciated.thanks
我猜你的 iOS 应用程序功能有问题。我认为您的 reply
闭包很可能没有被调用。我会首先尝试简化您的逻辑,以确保 reply
实际上正确返回。然后你就可以正确地传递数据了。我将首先简单地说明以下逻辑:
catecontroller.swift
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
WKInterfaceController.openParentApplication(["dummy": "dictionary"]) { reply, error in
println("Reply: \(reply)")
println("Error: \(error)")
}
}
AppDelegate.swift
func application(
application: UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!)
{
// Worry about this logic later...
reply(["test": "value"])
}
一旦你有了这个简化版本正确无误地传递数据,那么你就可以添加数据传递逻辑了。
代码: AppDelegate.swift
func application(application:UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!)
{
let entityDescription =
NSEntityDescription.entityForName("Quote",
inManagedObjectContext: managedObjectContext!)
let request = NSFetchRequest()
request.entity = entityDescription
let pred = NSPredicate(format: "(quoteDate = %@)", "2015-03-08")
request.predicate = pred
var error: NSError?
var objects = managedObjectContext?.executeFetchRequest(request,
error: &error)
if let results = objects {
if results.count > 0 {
arrQuotes = NSMutableArray()
for(var i=0;i<results.count;i++){
let match = results[i] as! NSManagedObject
var quote = match.valueForKey("quote") as! NSString
arrQuotes.addObject(quote)
}
var dict = ["test": arrQuotes]
reply(dict)
} else {
}
}
catecontroller.swift
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
arrQuotes = NSMutableArray()
var dict = ["test" : arrQuotes]
if !WKInterfaceController.openParentApplication(dict, reply: { (reply,error) -> Void in
println("\(reply)") //your reply data as Dictionary
self.arrQuotes = dict["test"]!
println("\(self.arrQuotes.count)")
}) {
println("ERROR")
}
我正在做一个示例 watchkit project.What 我正在尝试做的是从 iPhone 端获取数据并将其发送到 watchkit app.I 尝试 return 值作为 watchkit 端的 dictionary.but 数组,我得到的数组计数为 zero.I 不知道我哪里出错了?任何帮助都会提前 appreciated.thanks
我猜你的 iOS 应用程序功能有问题。我认为您的 reply
闭包很可能没有被调用。我会首先尝试简化您的逻辑,以确保 reply
实际上正确返回。然后你就可以正确地传递数据了。我将首先简单地说明以下逻辑:
catecontroller.swift
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
WKInterfaceController.openParentApplication(["dummy": "dictionary"]) { reply, error in
println("Reply: \(reply)")
println("Error: \(error)")
}
}
AppDelegate.swift
func application(
application: UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
reply: (([NSObject : AnyObject]!) -> Void)!)
{
// Worry about this logic later...
reply(["test": "value"])
}
一旦你有了这个简化版本正确无误地传递数据,那么你就可以添加数据传递逻辑了。