Yogi Yogi - 10 months ago 237
iOS Question

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.

Answer Source

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.