KenL KenL - 2 months ago 50
Swift Question

Swift 3 upgrade: Type 'Dictionary<NSObject, AnyObject>?' has no subscript members

I recently upgraded my app from Swift 2.3 to Swift 3.0 and when I did, I got the following error:


Type 'Dictionary<NSObject, AnyObject>?' has no subscript members



The function in which it appears is as follows:

class func getSSIDConnectionName() -> String? {
var currentSSID: String?
let interfaces = CNCopySupportedInterfaces()
if interfaces == nil {
print("Got nil up here")
return nil
}

let interfaces2:CFArray! = interfaces
for i in 0..<CFArrayGetCount(interfaces2) {
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces2, i)
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
if unsafeInterfaceData != nil {
let interfaceData = unsafeInterfaceData! as Dictionary!
currentSSID = interfaceData["SSID"] as? String
} else {
print("Got nil down here")
return nil
}
}

return currentSSID
}


I'm getting the error on the "current SSID =" line. This code was working fine in Swift 2.3, and unfortunately, I am not strong on things labeled as "unsafe," so if the answer delves into those regions, it be most helpful if you could explain it as simply as possible.

Thanks for reading!

Answer

Change

let interfaceData = unsafeInterfaceData! as Dictionary!

to

let interfaceData = unsafeInterfaceData! as NSDictionary

Reason: unsafeInterfaceData is a CFDictionary. A CFDictionary is directly castable to an NSDictionary, because they are toll-free bridged. That's good enough to get us subscripting, so we can use an expression like interfaceData["SSID"].

Comments