Swift 3中对成员'下标'的模糊引用

Yog*_*ogi 10 dictionary ios swift3 xcode8

我有一个用Swift 1.5构建的项目.当我将代码转换为swift 3.0时,它开始在下面的代码中的每个'if'语句中显示错误:

convenience init?(userInfo: [NSObject: AnyObject]) {
    guard let statusString = userInfo[ConnectionMessage.StatusKey] as? String else {
        return nil
    }
    guard let status = ConnectionStatus(string: statusString) else {
        return nil
    }

    guard let connectionId = userInfo[ConnectionMessage.ConnectionIdKey]?.longLongValue else {
        return nil
    }

    var ssid = ""

    if let newSsid = userInfo[ConnectionMessage.SSIDKey] as? String {
        ssid = newSsid
    }

    var password = ""
    if let pw = userInfo[ConnectionMessage.PasswordKey] as? String {
        password = pw
    }

    let buyerName = userInfo[ConnectionMessage.BuyerNameKey] as? String

    self.init(status: status, connectionId: connectionId, ssid: ssid, password: password, buyerName: buyerName)
}
Run Code Online (Sandbox Code Playgroud)

错误是

对成员下标的模糊引用

我尝试了在StackOverflow上找到的解决方案,但没有运气.请指导.

rma*_*ddy 20

改变userInfo[NSObject : AnyObject][String : AnyObject].这假定您的所有ConnectionMessage.xxxKey值都是String.

您还需要确保传递给userInfo参数的字典实际上是具有类型键的字典String.