ios C风格的for语句已被弃用,并将在Swift的未来版本中删除[duplicate]

92dk7w1h  于 2023-08-08  发布在  iOS
关注(0)|答案(1)|浏览(110)

此问题在此处已有答案

#warning: C-style for statement is deprecated and will be removed in a future version of Swift [duplicate](4个答案)
六年前就关门了。
我有一些代码在我的项目和下面的代码给予警告;

private var viewController: UIViewController? {
        for var next:UIView? = self.superview; next != nil; next = next?.superview {
            let responder = next?.nextResponder()
            if let vc = responder as? UIViewController {
                return vc
            }
        }
        return nil
    }

字符串
下面的警告行:

for var next:UIView? = self.superview; next != nil; next = next?.superview {


我该怎么解决?

3z6pesqy

3z6pesqy1#

下面是如何用while循环重写代码的方法:

var viewController: UIViewController? {
    var next: UIView? = self.superview
    while next != nil {
        let responder = next?.nextResponder()
        if let vc = responder as? UIViewController {
            return vc
        }
        next = next?.superview
    }
    return nil
}

字符串

相关问题