假設今天寫一段 if , else 的code
通常如下
int playerA;
int playerB;
if (playerA >playerB) {
highscore =playerA;
}
else {
highscore=playerB;
}
這是個任何程式語言都常會看到跟使用的方法
但是在 Objective C 裡面可以寫成
int highscore = (playerA>playerB)? playerA:playerB ;
短短的一行 就解決了上面數行
這邊寫的很有意思 其結構就如同 condition? true:false
假設 (playerA>playerB) 這個condition 結果為true
則 highscore=playerA
反之如果為false , highscore=playerB
我想很多人跟我一樣 在初期看到這行code時,一定也是一頭霧水阿
所以這邊寫出來給大家參考看看囉。
沒有留言:
張貼留言