Sending array of dictionary from iPhone to watchki

2019-09-05 15:42发布

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条回答
狗以群分
2楼-- · 2019-09-05 16:12

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.

查看更多
登录 后发表回答