【发布时间】:2018-01-29 02:52:06
【问题描述】:
我已经将我的 swift 版本从 2.3 迁移到 3,它自动转换了一些代码,下面是我遇到崩溃的情况,我尝试了一些选项但徒劳无功,
swift 2.3:工作正常
public func huntSuperviewWithClassName(className: String) -> UIView?
{
var foundView: UIView? = nil
var currentVeiw:UIView? = self
while currentVeiw?.superview != nil{
if let classString = String.fromCString(class_getName(currentVeiw?.dynamicType)){
if let classNameWithoutPackage = classString.componentsSeparatedByString(".").last{
print(classNameWithoutPackage)
if classNameWithoutPackage == className{
foundView = currentVeiw
break
}
}
}
currentVeiw = currentVeiw?.superview
}
return foundView
}
}
swift 3:不好
if let classString = String(validatingUTF8: class_getName(type(of:currentVeiw) as! AnyClass)) {
也试过这条线:
if let classString = String(describing: class_getName(type(of: currentVeiw) as! AnyClass)){
但它不起作用..
请指导我如何根据 swift 3 更正这条线:
if let classString = String.fromCString(class_getName(currentVeiw?.dynamicType)){
【问题讨论】:
-
你试过 if let classString = String(validatingUTF8: class_getName(type(of:currentVeiw) as?AnyClass))
-
Arvind 下面不好,我写了同一行
-
在 Swift 2.3 中 let classString = String.fromCString(class_getName(currentVeiw?.dynamicType)) 的值是多少?
-
返回类类型,例如 uiview、uibutton 等
-
@RibelynPunk 没有什么不同,在这里你是强制解开它而不是 as!尝试使用 as?
标签: ios swift3 swift2.3 dynamictype