
- 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 - fallthrough 語句
- 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 提供的一種特殊功能,用於訪問集合、序列和列表的元素。它是獲取或設定集合中值的便捷方式。我們還可以使用下標根據索引獲取或設定值。甚至類、結構體和列舉也可以定義下標。
單個型別可以有多個下標。我們可以根據傳遞給下標的索引值的型別使用適當的下標進行過載。我們可以根據需要定義一個可以接受單個引數或多個引數的下標。
下標宣告
我們可以使用 `subscript` 關鍵字定義**下標**,它可以接受一個或多個輸入引數並返回型別。它可以是讀寫或只讀的。它允許我們透過在例項名稱後編寫方括號中一個或多個值來對例項執行查詢。
語法
以下是下標的語法:
subscript(index: Int) -> Int { get { // Retrieve the value at the specified index } set(newValue) { // Set the value at the specified index } }
以下是隻讀下標的語法:
subscript(index: Int) -> Int { // Return subscript value here }
示例
Swift 程式,使用下標語法檢索值。
// Defining structure struct subexample { let decrementer: Int // Declaring subscript subscript(index: Int) -> Int { return decrementer / index } } // Creating instance of the structure let division = subexample(decrementer: 100) // Retrieving values using subscript syntax print("The number is divisible by \(division[9]) times") print("The number is divisible by \(division[2]) times") print("The number is divisible by \(division[3]) times") print("The number is divisible by \(division[5]) times") print("The number is divisible by \(division[7]) times")
輸出
它將產生以下輸出:
The number is divisible by 11 times The number is divisible by 50 times The number is divisible by 33 times The number is divisible by 20 times The number is divisible by 14 times
示例
Swift 程式,使用下標語法訪問值。
// Defining class class daysofaweek { private var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] // Declaring subscript subscript(index: Int) -> String { // Retrieve the value at the specified index get { return days[index] } // Set the value at the specified index set(newValue) { self.days[index] = newValue } } } // Creating instance of class var p = daysofaweek() // Accessing elements using subscript print(p[0]) print(p[1]) print(p[2]) print(p[3])
輸出
它將產生以下輸出:
Sunday Monday Tuesday Wednesday
下標選項
下標可以接受任何資料型別的單個或多個輸入引數,也可以返回任何資料型別的返回值。這些引數可以具有預設值。下標可以使用可變引數,但不能使用 in-out 引數。
定義多個下標稱為“下標過載”,其中類或結構體可以根據需要提供多個下標定義。這些多個下標是根據在下標括號內宣告的值的型別推斷的。
示例
Swift 程式,使用下標語法訪問值。
// Defining structure struct Matrix { let rows: Int, columns: Int var print: [Double] // Initializer to create a matrix init(rows: Int, columns: Int) { self.rows = rows self.columns = columns // Initializing the matrix with an array print = Array(repeating: 0.0, count: rows * columns) } // Subscript for accessing and modifying elements in the matrix subscript(row: Int, column: Int) -> Double { get { return print[(row * columns) + column] } set { print[(row * columns) + column] = newValue } } } // Creating an instance var mat = Matrix(rows: 3, columns: 3) // Modifying elements in the matrix using subscript notation mat[0, 0] = 1.0 mat[0, 1] = 2.0 mat[1, 0] = 3.0 mat[1, 1] = 5.0 // Accessing and printing elements from the matrix using subscript notation print("\(mat[0, 0])") print("\(mat[0, 1])") print("\(mat[1, 0])") print("\(mat[1, 1])")
輸出
它將產生以下輸出:
1.0 2.0 3.0 5.0
廣告