【问题标题】:Dynamic Type in Swift 3Swift 3 中的动态类型
【发布时间】: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


【解决方案1】:

编译器告诉你不能使用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
}

工作正常!

【讨论】:

  • 我在你的回答中做了一些修改,这给了我一个想法,现在这个修改过的答案工作正常......谢谢
【解决方案2】:
if let classString = String(describing: currentVeiw.self) 
{
}

【讨论】:

  • 这个函数不返回可选的,所以你不能在可选绑定中使用它,否则它会给出正确的结果
  • @user1000 如果不适用于字符串绑定...只让它返回这个,可选(
【解决方案3】:

只需这样做:

let classString = String(describing: type(of: currentVeiw!))

【讨论】:

    【解决方案4】:

    尝试以下方法:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 2017-08-03
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      相关资源
      最近更新 更多