Sending array of dictionary from iPhone to watchkit app

大兔子大兔子 提交于 2019-12-12 06:12:03

问题


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

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/28833265/sending-array-of-dictionary-from-iphone-to-watchkit-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!