什么'!self'在Swift中意味着什么

时间:2015-07-09 20:00:29

标签: ios swift swift2

this tutorial我看到了这种“!” +自我操作,它在Swift中意味着什么?

override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
  self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled())
  self.transitionContext?.viewControllerForKey(UITransitionContextFromViewControllerKey)?.view.layer.mask = nil
}


!self.transitionContext!.transitionWasCancelled()  //returns true
self.transitionContext!.transitionWasCancelled()   // without ! , return false

2 个答案:

答案 0 :(得分:5)

你可以在它周围放一个(心理)括号:

!(self.transitionContext!.transitionWasCancelled())

这意味着“不符合逻辑”。因此,如果self.transitionContext!.transitionWasCancelled() = true,请将false传递给函数completeTransition,反之亦然。

!中的self.transitionContext!在Swift中为optional unwrapping,但与逻辑无关。

答案 1 :(得分:1)

It's not a Swift-specific thing, it's the not operator. I.E. true == !false