最近我将我的应用从Swift 2.3升级到了Swift 3.0,但是当我这么做时,出现了以下错误:
Type 'Dictionary<NSObject, AnyObject>?' has no subscript members
它出现在下面的函数中:
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
}
我在“current SSID =”这一行代码中遇到了错误。在Swift 2.3版本中,这段代码能够正常工作,然而,我并不擅长处理标记为“unsafe”的操作,如果您的答案涉及到这方面的内容,请尽可能简单易懂地解释一下。
谢谢阅读!