有人知道有没有办法使用某种速记的速记?更具体地说,在诸如IF语句之类的东西中省略大括号
if num == 0 // Do something
代替
if num == 0 { // Do something }
当你有几个嵌套的IF,这些大括号变得相当空间消耗.
PS.我知道我可以做以下事情:
if num == 0 { // Do something }
但是,如果有可能的话,我仍然很好奇
你可以这样做:
原文链接:https://www.f2er.com/swift/319718.htmllet x = 10,y = 20; let max = (x < y) ? y : x ; // So max = 20
还有这么多有趣的事情:
let max = (x < y) ? "y is greater than x" : "x is greater than y" // max = "y is greater than x" let max = (x < y) ? true : false // max = true let max = (x > y) ? func() : anotherFunc() // max = anotherFunc() (x < y) ? func() : anotherFunc() // code is running func()
以下堆栈:http://codereview.stackexchange.com可以更好的为您的问题;)
编辑:注意三元运算符
By doing nothing more than replacing the ternary operator with an if else statement,the build time was reduced by 92.9%.
https://medium.com/@RobertGummesson/regarding-swift-build-time-optimizations-fc92cdd91e31#.42uncapwc