Swift 3 中的动态类型
Dynamic Type in Swift 3
我已经将我的 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
}
}
swift3:Not很好
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(describing: currentVeiw.self)
{
}
尝试以下操作:
public func huntSuperviewWithClassName(className: String) -> UIView?
{
var foundView: UIView? = nil
var currentVeiw:UIView? = self
while currentVeiw?.superview != nil{
let classString = String(describing: type(of: currentVeiw?.classForCoder))
if let classNameWithoutPackage = classString.components(separatedBy:".").first {
print(classNameWithoutPackage)
if classNameWithoutPackage == className {
foundView = currentVeiw
break
}
}
currentVeiw = currentVeiw?.superview
}
return foundView
}
只需这样做:
let classString = String(describing: type(of: currentVeiw!))
编译器告诉您不能使用 if let
,因为它完全没有必要。您没有任何可选项要解包。if let
专门用于解包选项。
public func huntSuperviewWithClassName(className: String) -> UIView?
{
var foundView: UIView? = nil
var currentVeiw:UIView? = self
while currentVeiw?.superview != nil{
let classString = NSStringFromClass((currentVeiw?.classForCoder)!)
if let classNameWithoutPackage = classString.components(separatedBy:".").last{
print(classNameWithoutPackage)
if classNameWithoutPackage == className{
foundView = currentVeiw
break
}
}
}
currentVeiw = currentVeiw?.superview
}
return foundView
}
工作正常!
我已经将我的 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
}
}
swift3:Not很好
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(describing: currentVeiw.self)
{
}
尝试以下操作:
public func huntSuperviewWithClassName(className: String) -> UIView?
{
var foundView: UIView? = nil
var currentVeiw:UIView? = self
while currentVeiw?.superview != nil{
let classString = String(describing: type(of: currentVeiw?.classForCoder))
if let classNameWithoutPackage = classString.components(separatedBy:".").first {
print(classNameWithoutPackage)
if classNameWithoutPackage == className {
foundView = currentVeiw
break
}
}
currentVeiw = currentVeiw?.superview
}
return foundView
}
只需这样做:
let classString = String(describing: type(of: currentVeiw!))
编译器告诉您不能使用 if let
,因为它完全没有必要。您没有任何可选项要解包。if let
专门用于解包选项。
public func huntSuperviewWithClassName(className: String) -> UIView?
{
var foundView: UIView? = nil
var currentVeiw:UIView? = self
while currentVeiw?.superview != nil{
let classString = NSStringFromClass((currentVeiw?.classForCoder)!)
if let classNameWithoutPackage = classString.components(separatedBy:".").last{
print(classNameWithoutPackage)
if classNameWithoutPackage == className{
foundView = currentVeiw
break
}
}
}
currentVeiw = currentVeiw?.superview
}
return foundView
}
工作正常!