不推荐使用C-style for语句,将在未来的Swift版本中删除

时间:2016-09-07 10:20:58

标签: ios swift

您好我的项目中有一些代码,下面的代码会发出警告;

 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 = next?.superview {

我该如何解决?

1 个答案:

答案 0 :(得分:1)

以下是如何使用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
}