简体   繁体   中英

Call different method depending on Bool without if-statement in Swift

Is it possible to call one or another method depending a Bool value? I wish to do this:

var isLocked: Bool
{
    didSet
    {
        // This is not Swift but indicates what I'm looking for.
        self.activityIndicator.(isLocked ? startAnimating() : stopAnimating())
    }
}

I'm looking to do this using existing Swift 2 (or 3) language features; without class extensions.

Possibly a duplicate, but couldn't find.

How about:

didSet {
    isLocked ? self.activityIndicator.startAnimating() :
        self.activityIndicator.stopAnimating()
}

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