当前位置: 动力学知识库 > 问答 > 编程问答 >

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()
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: