Swift变量声明麻烦
Swift variable declaration trouble
我正在遵循使用此语法声明变量的指南:
let fromView = presenting ? toView : transitionContext.view(forKey: UITransitionContextFromViewKey)
但我不明白语法:特别是问号和冒号(presenting
是一个布尔变量)。
这允许您基于布尔表达式声明变量。它可以节省时间,因为您不必写出各种 if else 语句。您问题中的上述表达式是根据布尔值声明变量。如果呈现为真,则 fromView 将设置为 toView(在冒号左侧)。如果呈现为假,则 fromView 设置为 transitionContext.view(forKey: UITransitionContextFromViewKey) 或冒号右侧
我正在遵循使用此语法声明变量的指南:
let fromView = presenting ? toView : transitionContext.view(forKey: UITransitionContextFromViewKey)
但我不明白语法:特别是问号和冒号(presenting
是一个布尔变量)。
这允许您基于布尔表达式声明变量。它可以节省时间,因为您不必写出各种 if else 语句。您问题中的上述表达式是根据布尔值声明变量。如果呈现为真,则 fromView 将设置为 toView(在冒号左侧)。如果呈现为假,则 fromView 设置为 transitionContext.view(forKey: UITransitionContextFromViewKey) 或冒号右侧