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
廣告