Skip to content

Dictionary

   

A Swift dictionary array. Describes how to create and call a dictionary array.
I like that different people use different ways of saying things like dictionary arrays, hashes, maps, etc.

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 初期化
var personDic: Dictionary<String, String> = ["height": "168", "wheight": "61", "like": "kabigon"]
print(personDic)
//["wheight": "61", "height": "168", "like": "kabigon"]
// データ追加
personDic["age"] = "24"
print(personDic)
//["wheight": "61", "height": "168", "age": "24", "like": "kabigon"]
// 任意のキーの値を取り出す
//存在しないキーにアクセスすると、?? 以降が呼ばれる
print(personDic["height"] ?? "200") //168
print(personDic["country"] ?? "japan") //japan
//要素数
print(personDic.count)
//4
// 辞書配列のKeyを取得する
let personDickeys: Array = Array(personDic.keys)
print(personDickeys) //
["wheight", "height", "age", "like"]
// 辞書配列のValueを取得する
let personDicValues: Array = Array(personDic.values)
print(personDicValues)
// ["61", "168", "24", "kabigon"]
// 指定したkeyのデータを削除
personDic.removeValue(forKey: "height")
print(personDic)
// ["wheight": "61", "age": "24", "like": "kabigon"]
}
}

  1. Array