SwiftDeveloper SwiftDeveloper - 3 months ago 12
iOS Question

C-style for statement is deprecated and will be removed in a future version of Swift

Hello I have some codes in my project and under below codes gives warning;

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
}


Warning line under below


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


How can i fix it ?

Answer

Here how you rewrite your code with while cycle:

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
}