加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

减少Swift中的括号数

发布时间:2020-12-14 05:36:03 所属栏目:百科 来源:网络整理
导读:有人知道有没有办法使用某种速记的速记?更具体地说,在诸如IF语句之类的东西中省略大括号 if num == 0 // Do something 代替 if num == 0{ // Do something} 当你有几个嵌套的IF,这些大括号变得相当空间消耗. PS.我知道我可以做以下事情: if num == 0 { //
有人知道有没有办法使用某种速记的速记?更具体地说,在诸如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 = 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读