简体   繁体   中英

Variable used within its own initial value in Swift 5

I try to write an animation with Swift 5, following are some codes

let animations:(() -> Void) = {
    self.keyboardOPT.transform = CGAffineTransform(translationX: 0,y: -deltaY)
    if duration > 0 {
        let options = UIView.AnimationOptions(rawValue: UInt((userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
        UIView.animate(withDuration: duration, delay: 0, options: options, animations: animations, completion: nil)
    } else {
        animations()
    }
}

But in animations: animations and animations() it shows error:

Variable used within its own initial value

You can not call itself when initializing. You can achieve it like this also.

var animations:(() -> Void)!

animations = {
    animations()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM