无法将objectForKey - [String:AnyObject]作为字典

时间:2016-04-19 13:07:47

标签: ios swift

我正在使用手表连接来接收消息(在objectiveC中生成的字典)。 被调用的委托函数(swift)并不想正确解释所接收的字典。

在我的dict中找不到ObjectForKey。是什么原因? 感谢

   func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
        print("\nWC watch:\n Did receive didReceiveMessage: \(message)")
        var messageDict = message as Dictionary<String, AnyObject> //here I wanted to cast to Dictionary
        let newState = messageDict.objectForKey(WatchManagerMessageMaskStateKey) //but the messageDict does not know objectForKey
}

1 个答案:

答案 0 :(得分:2)

[String : AnyObject]Dictionary<String, AnyObject>是完全相同的两个符号。

objectForKey属于NSDictionary,用于Swift原生类型使用密钥订阅 您可以将结果转换为更具体的内容。

func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
    print("\nWC watch:\n Did receive didReceiveMessage: \(message)")
    let newState = message[WatchManagerMessageMaskStateKey]
    ...
}