解包选项可以通过几种不同的方式完成,但我发现自己只使用一种方式.
func keyboardDidShow(keyboardRect: CGRect?) {
var height: CGFloat?
if let rectHeight = keyboardRect?.height {
height = rectHeight
} else {
height = 0
}
let insets = UIEdgeInsets(top: 0, left: 0, bottom: height!, right: 0)
///...clipping rest of method
}
Run Code Online (Sandbox Code Playgroud)
你会如何减少这里的线数?
在此特定示例中,您可以使用nil-coalescing运算符 ??:
func keyboardDidShow(keyboardRect: CGRect?) {
let height = keyboardRect?.height ?? 0
let insets = UIEdgeInsets(top: 0, left: 0, bottom: height, right: 0)
///...clipping rest of method
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |