Last active
May 11, 2017 09:01
-
-
Save takoikatakotako/02421a9e6c9b5d6c368b3c3f2e929c6c to your computer and use it in GitHub Desktop.
Dictionaryの基本的な使い方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment