我正在遵循使用此语法声明变量的指南:
let fromView = presenting ? toView : transitionContext.view(forKey: UITransitionContextFromViewKey)
但我不明白语法:特别是问号和冒号(presenting
是一个 bool 变量)。
最佳答案
这允许您基于 bool 表达式声明变量。它节省了时间,因为您不必编写各种 if else 语句。您问题中的上述表达式是根据 bool 值声明变量。如果 Presenting 为 true,则 fromView 将设置为 toView(位于冒号左侧)。如果呈现为 false,则 fromView 被设置为transitionContext.view(forKey: UITransitionContextFromViewKey) 或冒号右侧
关于Swift 变量声明麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38223362/