Swift 辞書

Swift の辞書は、順序付けられていない同じ型のデータの集合を格納するために使用されます。Swift の辞書は要素の型を強制的にチェックし、型が異なる場合はエラーを報告します。

Swift の辞書では、各値(value)が一意のキー(key)に関連付けられており、キーは辞書内のこの値のデータの識別子として機能します。

配列内のデータ項目とは異なり、辞書内のデータ項目には特定の順序がありません。識別子(キー)を介してデータにアクセスする必要がある場合に辞書を使用します。この方法は、現実世界で辞書を使って文字の意味を調べる方法とほぼ同じです。

Swift の辞書のキーには型の制限がなく、整数型や文字列にすることができますが、一意である必要があります。

辞書を作成して変数に代入すると、作成された辞書は変更可能になります。つまり、辞書を作成した後、追加、削除、変更によって辞書内の項目を変更できます。辞書を定数に代入すると、辞書は変更できず、辞書のサイズと内容も変更できません。


辞書の作成

次の構文を使用して、特定の型の空の辞書を作成できます:

var someDict =  [KeyType: ValueType]()

以下は、キーの型が Int、値の型が String の空の辞書を作成する簡単な構文です:

var someDict = [Int: String]()

以下は辞書を作成するインスタンスです:

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

辞書へのアクセス

辞書のインデックスに基づいて配列の要素にアクセスできます。構文は次のとおりです:

var someVar = someDict[key]

次のインスタンスを通じて、辞書の作成、初期化、アクセスの方法を学ぶことができます:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var someVar = someDict[1]

print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

上記のプログラムを実行した出力結果は次のとおりです:

key = 1 的值为 Optional("One")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

辞書の変更

私たちは次を使用できますupdateValue(forKey:)辞書の内容を追加または更新します。キーが存在しない場合は値を追加し、存在する場合はキーに対応する値を変更します。updateValue(_:forKey:) メソッドは Optional 値を返します。インスタンスは次のとおりです:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var oldVal = someDict.updateValue("One 新的值", forKey: 1)

var someVar = someDict[1]

print( "key = 1 旧的值 \(oldVal)" )
print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

上記のプログラムを実行した出力結果は次のとおりです:

key = 1 旧的值 Optional("One")
key = 1 的值为 Optional("One 新的值")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

指定したキーを使用して辞書の値を変更することもできます。次のようになります:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var oldVal = someDict[1]
someDict[1] = "One 新的值"
var someVar = someDict[1]

print( "key = 1 旧的值 \(oldVal)" )
print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

上記のプログラムを実行した出力結果は次のとおりです:

key = 1 旧的值 Optional("One")
key = 1 的值为 Optional("One 新的值")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

Key-Value ペアの削除

私たちは次を使用できますremoveValueForKey()メソッドを使用して辞書のキーと値のペアを削除します。キーが存在する場合、このメソッドは削除された値を返し、存在しない場合は nil を返します。インスタンスは次のとおりです:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var removedValue = someDict.removeValue(forKey: 2)

print( "key = 1 的值为 \(someDict[1])" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

上記のプログラムを実行した出力結果は次のとおりです:

key = 1 的值为 Optional("One")
key = 2 的值为 nil
key = 3 的值为 Optional("Three")

キーの値を nil に指定して、キーと値のペアを削除することもできます。インスタンスは次のとおりです:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

someDict[2] = nil

print( "key = 1 的值为 \(someDict[1])" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

上記のプログラムを実行した出力結果は次のとおりです:

key = 1 的值为 Optional("One")
key = 2 的值为 nil
key = 3 的值为 Optional("Three")

辞書の走査

私たちはループを使用してfor-in辞書内のキーと値のペアを走査できます。実例は次のとおりです:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

for (key, value) in someDict {
   print("字典 key \(key) -  字典 value \(value)")
}

上記のプログラムを実行した出力結果は次のとおりです:

字典 key 2 -  字典 value Two
字典 key 3 -  字典 value Three
字典 key 1 -  字典 value One

enumerate() メソッドを使用して辞書を走査することもできます。返されるのは辞書のインデックスと (キー, 値) のペアです。インスタンスは次のとおりです:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

for (key, value) in someDict.enumerated() {
    print("字典 key \(key) -  字典 (key, value) 对 \(value)")
}

上記のプログラムを実行した出力結果は次のとおりです:

字典 key 0 -  字典 (key, value) 对 (2, "Two")
字典 key 1 -  字典 (key, value) 对 (3, "Three")
字典 key 2 -  字典 (key, value) 对 (1, "One")

辞書を配列に変換

辞書のキーと値のペアを抽出して、独立した配列に変換できます。インスタンスは次のとおりです:

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

let dictKeys = [Int](someDict.keys)
let dictValues = [String](someDict.values)

print("输出字典的键(key)")

for (key) in dictKeys {
    print("\(key)")
}

print("输出字典的值(value)")

for (value) in dictValues {
    print("\(value)")
}

上記のプログラムを実行した出力結果は次のとおりです:

输出字典的键(key)
2
3
1
输出字典的值(value)
Two
Three
One

count プロパティ

私たちは読み取り専用のcountプロパティを使用して、辞書にいくつのキーと値のペアがあるかを計算できます:

import Cocoa

var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2:[Int:String] = [4:"Four", 5:"Five"]

print("someDict1 含有 \(someDict1.count) 个键值对")
print("someDict2 含有 \(someDict2.count) 个键值对")

上記のプログラムを実行した出力結果は次のとおりです:

someDict1 含有 3 个键值对
someDict2 含有 2 个键值对

isEmpty プロパティ

私たちは読み取り専用プロパティを通じてisEmpty辞書が空かどうかを判断し、ブール値を返します:

import Cocoa

var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2:[Int:String] = [4:"Four", 5:"Five"]
var someDict3:[Int:String] = [Int:String]()

print("someDict1 = \(someDict1.isEmpty)")
print("someDict2 = \(someDict2.isEmpty)")
print("someDict3 = \(someDict3.isEmpty)")

上記のプログラムを実行した出力結果は次のとおりです:

someDict1 = false
someDict2 = false
someDict3 = true
その他の拡張