简体   繁体   English

无法下标'[NSObject:AnyObject]类型的值?” 索引类型为'String'的Swift错误

[英]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.

相关问题 不能下标'[NSObject:AnyObject]类型的值吗?索引类型为'String' - Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String' 无法用索引类型“字符串”下标“ [NSObject:AnyObject]”类型的值 - Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'String' “无法下标‘字典’类型的值<nsobject, anyobject> ' 索引类型为 'String' 错误</nsobject,> - "Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String" error 错误无法下标“字典”类型的值 <NSObject, AnyObject> &#39;的索引类型为&#39;String&#39; - Error Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String' 无法用索引为“ NSKeyValueChangeKey”的下标“ [NSObject:AnyObject]”的值 - Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'NSKeyValueChangeKey' Swift:无法为[String:NSObject]的下标值加上索引类型吗? - Swift: cannot subscript value of [String:NSObject] with index of type? 无法下标[AnyObject]的值? 索引类型为String - Cannot subscript a value of [AnyObject]? with an index of type String Swift中的NSDictionary:不能下标“AnyObject”类型的值吗? 索引类型为&#39;Int&#39; - NSDictionary in Swift:Cannot subscript a value of type 'AnyObject?' with a index of type 'Int' 不能下标类型为[String:AnyObject]的值?索引类型为String - Cannot subscript a value of type [String: AnyObject]? with an index of type String 无法使用索引类型为“String”的类型&#39;[String:AnyObject]&#39;下标值 - Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM