iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 6
1
Software Development

30 天上手 iOS App 開發系列 第 6

30 天上手 iOS App 開發 Day6

DAY6. swift有哪些基本的語法

For-in

Swift的for迴圈會自動幫你宣告變數名稱,等到離開這個迴圈之後會自動釋放記憶體空間
其中的0...9代表從『0到9』,如果是0...<9代表『從0到8』
https://ithelp.ithome.com.tw/upload/images/20171225/2010750601A6Yh1yWX.png
以下是比較進階版,利用兩個變數製作一個9x9乘法表,因為Print裡面預設的terminator預設是\n 所以會自動換行
如果要在字串裡面合併變數 需要打()
https://ithelp.ithome.com.tw/upload/images/20171225/20107506Zs7JYJruwB.png
也可以利用for迴圈來print出陣列裡面的東西
https://ithelp.ithome.com.tw/upload/images/20171225/20107506X4O1K6ikdI.png
While
當while迴圈的判斷條件是true的時候,迴圈就會執行
下面的number從5加到10,因為不再小於10,所以跳出迴圈
https://ithelp.ithome.com.tw/upload/images/20171225/20107506NNg8CU7R9Q.png
迴圈中continue與break的用法
迴圈中可以執行continue來結束剩下的程式碼,再回到程式開始處,所以當i是5的時候並不會執行到第24行,而是直接跳回20行
https://ithelp.ithome.com.tw/upload/images/20171225/20107506A8uLGBPEK9.png
break可以直接跳出迴圈
https://ithelp.ithome.com.tw/upload/images/20171225/201075060VTHE4akqD.png
當i=5的時候,執行到break直接跳出for迴圈


上一篇
30 天上手 iOS App 開發 Day5
下一篇
30 天上手 iOS App 開發 Day7
系列文
30 天上手 iOS App 開發28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言