1
遇到了同樣的問題與Swift 3.1 deprecates initialize(). How can I achieve the same thing?,@Jordan史密斯的解決方案是非常令人印象深刻,當時我感興趣的實現,但遇到了在實踐中一些麻煩,下面是一些關鍵的代碼,看到的評論,爲什麼在UIViewController
日誌功能沒有被調用,它符合協議;爲什麼UIViewController
被抓住了,但T == UIViewController.self
是false
:在swift中對類型判斷感到困惑?
protocol Conscious {
static func awake()
}
/** extension */
extension UIViewController: Conscious {
static func awake() {
if self == UIViewController.self {
print(self, #function) // never came here, but seems should come
}
}
}
/** main */
private static let _operation: Void = {
let typeCount = Int(objc_getClassList(nil, 0))
let types = UnsafeMutablePointer<AnyClass?>.allocate(capacity: typeCount)
let autoreleasingTypes = AutoreleasingUnsafeMutablePointer<AnyClass?>(types)
objc_getClassList(autoreleasingTypes, Int32(typeCount))
for index in 0 ..< typeCount {
(types[index] as? Conscious.Type)?.awake()
let T = types[index]!
let vc = UIViewController()
print(T, vc.isKind(of: T), T == UIViewController.self)
/*
Strange things:
UIResponder true false
UIViewController true false(why is false)
UISearchController false false
*/
}
types.deallocate(capacity: typeCount)
}()
你有什麼問題嗎? – rmaddy
@rmaddy對不起, – quentinjin
OK,所以再次看到在代碼的註釋,你有什麼問題嗎? – rmaddy