为什么三元运算符如此之快? [英] Why ternary operator in swift is so picky?
本文介绍了为什么三元运算符如此之快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题很简单,但我只是找不到答案!
The question is very simple, but I just could not find the answer!
为什么不
return x == 0? "" : "Hello"
编译但
return x == 0 ? "" : "Hello"
是吗?
这真的很奇怪,因为所有其他运算符都不需要多余的空格.例如
This is really weird because all the other operators don't need an extra white space. e.g.
let x = 1+1
let y = 1 + 1
一样.
我认为这与可选内容有关.但是,当在变量上使用?
运算符时,必须像这样使用它:
I think it has something to do with optionals. But when you use a ?
operator on a variable, it must be used like this:
let s: String? = nil
let x = s?.startIndex
我的意思是它必须跟随另一个运算符,对吗?
I mean it must follow another operator, right?
推荐答案
我认为这与可选内容有关.
I think it has something to do with optionals.
查看全文