本文介绍了将字典数组从 iPhone 发送到 watchkit 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:AppDelegate.swift

Code: 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

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 项目.我想要做的是从 iPhone 端获取数据并将其发送到 watchkit 应用程序.我尝试将值作为字典数组返回.但在 watchkit 端我得到数组计数为零.我不知道我哪里出错了?任何帮助将不胜感激.提前致谢

I am doing a sample watchkit project.What i am trying to do is fetch data from iPhone side and send it to watchkit app.I try to return the value as array of dictionary.but in watchkit side i am getting array count as zero.I do not know where i went wrong?any help will be appreciated.thanks in advance

推荐答案

我猜你的 iOS 应用功能有问题.我认为您的 reply 闭包很可能没有被调用.我会首先尝试简化您的逻辑,以确保 reply 实际上正确返回.然后,您可以正确地传递数据.我首先将逻辑简化为以下内容:

I would guess that you have some trouble in your iOS app function. I think your reply closure is most likely not being called. I would try to simplify your logic first to make sure the reply is actually coming back correctly. Then you can work on passing the data correctly. I would simply the logic to the following first:

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"])
}

一旦您有了这个简化版本正确传递数据且没有错误,那么您就可以添加数据传递逻辑.

Once you have this simplified version passing the data correctly with no errors, then you can add the data passing logic.

这篇关于将字典数组从 iPhone 发送到 watchkit 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:47