如何打印从手表连接即时消息返回的数组

时间:2016-10-10 00:20:13

标签: arrays swift watchconnectivity

我的Iphone正在发送一个阵列作为手表连接消息。如何打印手表上收到的阵列?我收到的是“nil'当我尝试打印数组时。

当我打印手表信息数组时,我在手机上发送,它会返回: [" message1":["用户名":"访客用户"," titleItem":" Hillary广告镜"] ],

手机上发送消息的代码是:

func sendMessage(){

    var messageDataArray1 = ["username":"Guest User",  "titleItem":titleItem!]

    // Send message
    if (WCSession.defaultSession().reachable) {
        print("sending watch message array:")

        var message1 = ["message1":messageDataArray1]

        print(message1)

        WCSession.defaultSession().sendMessage(message1,
                                               replyHandler: { (reply) -> Void in
                                                dispatch_async(dispatch_get_main_queue(), {
                                                    print("received return watch msg")
                                                })
            },
                                               errorHandler: { (error) -> Void in
                                                dispatch_async(dispatch_get_main_queue(), {
                                                    //self.receivedMessageLabel.setText("error")
                                                })
            }
        )       
    }
}

手表接收控制器上的代码是:

// MARK: - WCSessionDelegate
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    print("received Imessage from ph")
    if let msg = message["message1"] as? String {
        print(msg)
    }
}

1 个答案:

答案 0 :(得分:2)

message["message1"]会为您messageDataArray1提供[String:String]类型的字典,但您尝试将其转换为String,但始终会失败并返回{{} 1}}。

您应该将其转换为nil

[String:String]