gpt4 book ai didi

ios - Swift 3 中的动态类型

转载 作者:行者123 更新时间:2023-11-29 11:42:17 24 4
gpt4 key购买 nike

我已经将我的 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 因为它完全没有必要。您没有任何要解包的可选值。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
}

工作正常!

关于ios - Swift 3 中的动态类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45790180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com