Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swift3

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!

like image 541
KenL Avatar asked Dec 14 '22 03:12

KenL


2 Answers

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"].

like image 79
matt Avatar answered May 30 '23 21:05

matt


In addition to what @matt said, your code can be simplified considerably, in particular by casting the return value from CNCopySupportedInterfaces() to a Swift [String] array, and optional binding if let instead of testing against nil and forced unwrapping:

func getSSIDConnectionName() -> String? {

    guard let interfaces = CNCopySupportedInterfaces() as? [String] else {
        return nil
    }
    for ifname in interfaces {
        if let interfaceData = CNCopyCurrentNetworkInfo(ifname as CFString) as? [String: Any],
            let currentSSID = interfaceData["SSID"] as? String {
            return currentSSID
        }
    }
    return nil
}
like image 37
Martin R Avatar answered May 30 '23 21:05

Martin R