Swift-subscripts

提供:Dev Guides
移動先:案内検索

Swift-添え字

クラス、構造、および列挙のコレクション、シーケンス、およびリストの要素メンバーへのアクセスは、添え字を使用して実行されます。 これらの添え字は、インデックスを使用して値を保存および取得するために使用されます。 配列要素はsomeArray [index]の助けを借りてアクセスされ、Dictionaryインスタンスの後続のメンバー要素はsomeDicitonary [key]としてアクセスできます。

単一の型の場合、添字の範囲は単一から複数の宣言までです。 適切な添え字を使用して、添え字に渡されるインデックス値のタイプをオーバーロードできます。 下付き文字は、入力データ型宣言のユーザー要件に応じて、単一ディメンションから複数ディメンションまでの範囲もあります。

下付き宣言の構文とその使用法

計算されたプロパティの要約をしましょう。 下付き文字も、計算されたプロパティと同じ構文に従います。 タイプのインスタンスを照会する場合、サブスクリプトは角括弧内に書き込まれ、その後にインスタンス名が続きます。 添え字構文は、「インスタンスメソッド」および「計算プロパティ」構文と同じ構文構造に従います。 「添字」キーワードは添字の定義に使用され、ユーザーは単一または複数のパラメーターを戻り値の型で指定できます。 添字には読み取り/書き込みまたは読み取り専用のプロパティを設定でき、インスタンスは計算プロパティの「getter」および「setter」プロパティを使用して保存および取得できます。

構文

subscript(index: Int) −> Int {
   get {
     //used for subscript value declarations
   }
   set(newValue) {
     //definitions are written here
   }
}

例1

struct subexample {
   let decrementer: Int
   subscript(index: Int) -> Int {
      return decrementer/index
   }
}
let division = subexample(decrementer: 100)

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

例2

class daysofaweek {
   private var days = ["Sunday", "Monday", "Tuesday", "Wednesday",
      "Thursday", "Friday", "saturday"]
   subscript(index: Int) -> String {
      get {
         return days[index]
      }
      set(newValue) {
         self.days[index] = newValue
      }
   }
}
var p = daysofaweek()

print(p[0])
print(p[1])
print(p[2])
print(p[3])

プレイグラウンドを使用して上記のプログラムを実行すると、次の結果が得られます-

Sunday
Monday
Tuesday
Wednesday

下付き文字のオプション

添え字は単一から複数の入力パラメーターを取り、これらの入力パラメーターも任意のデータ型に属します。 また、変数および可変パラメータを使用できます。 添え字は、デフォルトのパラメーター値を提供したり、入出力パラメーターを使用したりすることはできません。

複数の添え字の定義は、「添え字のオーバーロード」と呼ばれ、クラスまたは構造は必要に応じて複数の添え字定義を提供できます。 これらの複数の添え字は、添え字ブレース内で宣言された値のタイプに基づいて推測されます。

struct Matrix {
   let rows: Int, columns: Int
   var print: [Double]
   init(rows: Int, columns: Int) {
      self.rows = rows
      self.columns = columns
      print = Array(count: rows *columns, repeatedValue: 0.0)
   }
   subscript(row: Int, column: Int) -> Double {
      get {
         return print[(row* columns) + column]
      }
      set {
         print[(row *columns) + column] = newValue
      }
   }
}
var mat = Matrix(rows: 3, columns: 3)

mat[0,0] = 1.0
mat[0,1] = 2.0
mat[1,0] = 3.0
mat[1,1] = 5.0

print("\(mat[0,0])")

プレイグラウンドを使用して上記のプログラムを実行すると、次の結果が得られます-

1.0

Swift 4の添字は、適切なデータ型の単一のパラメーターから複数のパラメーター宣言をサポートします。 プログラムは、「Double」データ型を格納するために、「Matrix」構造を2* 2次元配列行列として宣言します。 Matrixパラメーターには、行と列を宣言するための整数データ型が入力されます。

Matrixの新しいインスタンスは、以下に示すように、行と列のカウントを初期化に渡すことで作成されます。

var mat = Matrix(rows: 3, columns: 3)

行列値は、下に示すように、行と列の値を下付き文字にコンマで区切って渡すことで定義できます。

mat[0,0] = 1.0
mat[0,1] = 2.0
mat[1,0] = 3.0
mat[1,1] = 5.0