[英]Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String' Swift Error
My cod is that one and this error appears in the last line of the class, what should I do to solve it? 我的鳕鱼就是那个,这个错误出现在课程的最后一行,我该怎么做才能解决呢?
public class SSID {
class func fetchSSIDInfo() -> String {
var currentSSID = ""
if let interfaces = CNCopySupportedInterfaces() {
for i in 0..<CFArrayGetCount(interfaces) {
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, 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
}
}
}
return currentSSID
}
}
The only lines that uses a subscript are: 使用下标的唯一行是:
let interfaceData = unsafeInterfaceData! as Dictionary!
currentSSID = interfaceData?["SSID"] as String
Accessing with a string like this: interfaceData?["SSID"]
requires you to define your dictionary differently, your current dictionary is declared as [NSObject: AnyObject]
but to use a String subscript it would need to be [String: AnyObject]
使用这样的字符串进行访问: interfaceData?["SSID"]
要求您以不同的方式定义字典,当前字典被声明为[NSObject: AnyObject]
但要使用String下标,则必须为[String: AnyObject]
Try changing this line to: 尝试将此行更改为:
let interfaceData = unsafeInterfaceData! as [String: AnyObject]
Note: Best to use if let rather than force unwrapping with !
注意:如果让用而不是用!
强制展开,则最好使用!
You can try 你可以试试
if let interfaceData = unsafeInterfaceData! as? [String:Any]
{
currentSSID = interfaceData["SSID"] as? String
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.