【发布时间】:2020-02-05 18:47:13
【问题描述】:
所以这是我从另一个问题中找到的一些代码,它强制从 Any 向下转换为 CFString
import SystemConfiguration.CaptiveNetwork
func fetchSSIDInfo() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
// is there any way to remove the force downcast below ????
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
}
}
}
return ssid
}
您必须使用强制向下转换还是有其他方法?处理旧的 Corefoundation 对象时是否有特殊规则?因为使用 NSObjects 似乎要简单得多。
【问题讨论】:
-
最好添加一个链接到问题(或答案),从那里获得您所询问的代码。
-
尽量将 CoreFoundation 类型直接转换为 Swift 类型,而不是 typeless
NSArray/NSDictionary例如if let interfaces = CNCopySupportedInterfaces() as? [CFString] { ...
标签: swift casting downcast cfstring