Failing cast in Swift from Any? to protocol

前端 未结 4 1880
执念已碎
执念已碎 2020-11-27 22:23

FYI: Swift bug raised here: https://bugs.swift.org/browse/SR-3871


I\'m having an odd problem where a cast isn\'t working, but the console shows it as the corre

4条回答
  •  暖寄归人
    2020-11-27 22:51

    Still not fixed. My favorite and easiest workaround is by far chain casting:

    if let instance = sender as AnyObject as? MyProtocol {
    
    }
    

提交回复
热议问题