减少Swift中的括号数

减少Swift中的括号数,第1张

概述有人知道有没有办法使用某种速记的速记?更具体地说,在诸如IF语句之类的东西中省略大括号 if num == 0 // Do something 代替 if num == 0{ // Do something} 当你有几个嵌套的IF,这些大括号变得相当空间消耗. PS.我知道我可以做以下事情: if num == 0 { // Do something } 但是,如果有可能的话,我仍 有人知道有没有办法使用某种速记的速记?更具体地说,在诸如IF语句之类的东西中省略大括号
if num == 0  // Do something

代替

if num == 0{  // Do something}

当你有几个嵌套的IF,这些大括号变得相当空间消耗.

PS.我知道我可以做以下事情:

if num == 0 {  // Do something }

但是,如果有可能的话,我仍然很好奇

你可以这样做:
let 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 = truelet 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

总结

以上是内存溢出为你收集整理的减少Swift中的括号数全部内容,希望文章能够帮你解决减少Swift中的括号数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1039701.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存