Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous reference to member 'subscript' in Swift 3

I have a project built in Swift 1.5. When I converted the code to swift 3.0 it started showing me errors in each 'if' statement in below code:

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)
}

The error is

Ambiguous reference to member subscript

I tried the solutions found on StackOverflow but no luck. Please guide.

like image 543
Yogi Avatar asked Oct 04 '16 06:10

Yogi


Video Answer


1 Answers

Change userInfo from [NSObject : AnyObject] to [String : AnyObject]. This assumes all of your ConnectionMessage.xxxKey values are String.

You also need to ensure that the dictionary you pass into the userInfo parameter is actually a dictionary with keys of type String.

like image 150
rmaddy Avatar answered Nov 15 '22 19:11

rmaddy