使用handleWatchKitExtensionRequest传递数组以观看应用程序

时间:2015-08-02 23:51:35

标签: ios swift nsarray watchkit

我终于设法将一些数据从父应用程序传递到我的Apple Watch应用程序,但是我现在想要传递一个数组。目前我使用了2个单独的字符串,但是我无法弄清楚如何使用2个数组。

以下是我在监视应用中的当前代码:

func reloadTable() {
    WKInterfaceController.openParentApplication(["pfquery_request": "pfquery_request"]) { userInfo, error in
        println("User Info: \(userInfo)")
        println("Error: \(error)")

        var data = (userInfo as NSDictionary)

        if let success = data["success"] as? NSNumber {
            if success.boolValue == true {
                var name = data.objectForKey("Name") as! String
                var waitTime = data.objectForKey("WaitTime") as! String
                println(name)
            }
        }
    }
}

在父应用中:

func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
    if let pfqueryRequest: AnyObject = (userInfo as? [String: AnyObject])?["pfquery_request"] {
        var name = ""
        var waitTime = ""

        var query = PFQuery(className: "Park")
        var objects = query.findObjects()

        if let objects = objects as? [PFObject] {
            for object in objects {
                name = (object.objectForKey("Name") as? String)!
                waitTime = (object.objectForKey("WaitTime") as? String)!
            }

            reply(["success": true, "name": name, "waitTime": waitTime])
        }
    }
}

有人能指出我正确的方向或提供一些示例代码吗?我正在寻找传递所有对象名称和waitTimes的数组。

谢谢!

1 个答案:

答案 0 :(得分:0)

这应该有效,但未经过测试!

if let pfqueryRequest: AnyObject = (userInfo as? [String: AnyObject])?["pfquery_request"] {
        var name : [String] = []
        var waitTime : [String] = []

        var query = PFQuery(className: "Park")
        var objects = query.findObjects()

        if let objects = objects as? [PFObject] {
            for object in objects {
                name += (object.objectForKey("Name") as? String)!
                waitTime += (object.objectForKey("WaitTime") as? String)!
            }

            reply(["success": true, "name": name, "waitTime": waitTime])
        }
    }