【问题标题】:Ambiguous reference to member 'subscript' in Swift 3Swift 3 中对成员“下标”的模糊引用
【发布时间】:2017-02-12 05:14:54
【问题描述】:

我有一个使用 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)
}

错误是

对成员下标的不明确引用

我尝试了在 StackOverflow 上找到的解决方案,但没有成功。请指导。

【问题讨论】:

    标签: ios dictionary swift3 xcode8


    【解决方案1】:

    userInfo[NSObject : AnyObject] 更改为 [String : AnyObject]。这假设您的所有ConnectionMessage.xxxKey 值都是String

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

    【讨论】:

    • 谢谢。知道了。你能解释一下为什么它在以前版本的 swift 中可以工作,为什么现在不能工作?
    • 我只学了 Swift 3.0,所以我不知道它在旧版本中是如何处理的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多