
- Swift 教程
- Swift - 首頁
- Swift - 概述
- Swift - 環境
- Swift - 基本語法
- Swift - 變數
- Swift - 常量
- Swift - 字面量
- Swift - 註釋
- Swift 運算子
- Swift - 運算子
- Swift - 算術運算子
- Swift - 比較運算子
- Swift - 邏輯運算子
- Swift - 賦值運算子
- Swift - 位運算子
- Swift - 其他運算子
- Swift 高階運算子
- Swift - 運算子過載
- Swift - 算術溢位運算子
- Swift - 恆等運算子
- Swift - 範圍運算子
- Swift 資料型別
- Swift - 資料型別
- Swift - 整數
- Swift - 浮點數
- Swift - Double
- Swift - 布林值
- Swift - 字串
- Swift - 字元
- Swift - 類型別名
- Swift - 可選值
- Swift - 元組
- Swift - 斷言和前提條件
- Swift 控制流
- Swift - 決策
- Swift - if 語句
- Swift - if...else if...else 語句
- Swift - if-else 語句
- Swift - 巢狀 if 語句
- Swift - switch 語句
- Swift - 迴圈
- Swift - for in 迴圈
- Swift - while 迴圈
- Swift - repeat...while 迴圈
- Swift - continue 語句
- Swift - break 語句
- Swift - fall through 語句
- Swift 集合
- Swift - 陣列
- Swift - 集合
- Swift - 字典
- Swift 函式
- Swift - 函式
- Swift - 巢狀函式
- Swift - 函式過載
- Swift - 遞迴
- Swift - 高階函式
- Swift 閉包
- Swift - 閉包
- Swift - 轉義和非轉義閉包
- Swift - 自動閉包
- Swift 面向物件程式設計
- Swift - 列舉
- Swift - 結構體
- Swift - 類
- Swift - 屬性
- Swift - 方法
- Swift - 下標
- Swift - 繼承
- Swift - 重寫
- Swift - 初始化
- Swift - 析構
- Swift 高階特性
- Swift - ARC 概述
- Swift - 可選鏈
- Swift - 錯誤處理
- Swift - 併發
- Swift - 型別轉換
- Swift - 巢狀型別
- Swift - 擴充套件
- Swift - 協議
- Swift - 泛型
- Swift - 訪問控制
- Swift - 函式與方法
- Swift - SwiftyJSON
- Swift - 單例類
- Swift 隨機數
- Swift 不透明型別和裝箱型別
- Swift 有用資源
- Swift - 線上編譯
- Swift - 快速指南
- Swift - 有用資源
- Swift - 討論
Swift - 註釋
註釋是程式中不會被編譯器編譯的特殊文字。註釋的主要目的是向我們解釋程式碼的特定行或整個程式中發生了什麼。程式設計師通常會添加註釋來解釋程式中程式碼行的作用。
或者我們可以說註釋是非可執行文字,它們就像給使用者或程式設計師的筆記或提醒。在 Swift 中,我們可以透過三種不同的方式定義註釋:
單行註釋
多行註釋
巢狀多行註釋
Swift 中的單行註釋
單行註釋用於在程式碼中新增只有一行的文字。單行註釋以雙斜槓 (//) 開頭。編譯器或直譯器總是忽略它們,並且不會影響程式的執行。
語法
以下是單行註釋的語法:
// Add your comment here
示例
新增單行註釋的 Swift 程式。這裡我們在程式中新增單行註釋來解釋 for-in 迴圈的工作原理。
import Foundation let num = 7 let endNum = 10 // For loop to display a sequence of numbers for x in num...endNum{ print(x) }
輸出
7 8 9 10
Swift 中的多行註釋
多行註釋用於在程式中顯示多行非可執行文字,以解釋特定程式碼行的作用,或由開發人員新增警告、註釋等。與其他程式語言一樣,在 Swift 中,多行註釋以斜槓後跟星號 (/*) 開頭,以星號後跟斜槓 (*/) 結尾。
語法
以下是多行註釋的語法:
/* Add your Mult-line comment here */
示例
新增多行註釋的 Swift 程式。這裡我們在程式中新增多行註釋來解釋如何新增兩個長度相同的陣列。
import Foundation let arr1 = [1, 4, 6, 2] let arr2 = [3, 5, 2, 4] var result = [Int]() /* Check the length of the array. If they are equal then we add them using the + operator and store the sum in the result array */ if arr1.count == arr2.count { for i in 0..<arr1.count { let sum = arr1[i] + arr2[i] result.append(sum) } print(result) } else { print("Arrays must of same length.") }
輸出
[4, 9, 8, 6]
Swift 中的巢狀多行註釋
從 Swift 4 開始,多行註釋中也包含一個新特性,即巢狀多行註釋。現在允許您巢狀或在另一個多行註釋中新增多行註釋。即使程式碼塊包含多行註釋,它也可以輕鬆註釋掉許多程式碼塊。
語法
以下是巢狀多行註釋的語法:
/* Add your multi-line comment. /* Add your nested multi-line comment. */ End multi-line comment */
示例
新增巢狀多行註釋的 Swift 程式。這裡我們在程式中新增巢狀多行註釋來新增新增兩個陣列的替代程式碼。
import Foundation let arr1 = [1, 4, 6, 2] let arr2 = [3, 5, 2, 4] var result = [Int]() /* Checks the length of the array. If they are equal then we add them using the + operator and store the sum in the result array /*You can also use the following code to add two arrays: if arr1.count == arr2.count { let result = zip(arr1, arr2).map(+) print(result) */ */ if arr1.count == arr2.count { for i in 0..<arr1.count { let sum = arr1[i] + arr2[i] result.append(sum) } print(result) } else { print("Arrays must of same length.") }
輸出
[4, 9, 8, 6]
廣告