Swift入門メモ -タプル・配列・辞書-
05 タプルを使ってみよう
- タプル->複数の値をまとめて渡すときによく使われる
// タプル let error = (404, "not found") error.0 // 404 error.1 // not found // 名前を付ける let error2 = (code: 404, msg: "not found") error2.0 // 404 error2.code // 404 error2.1 // not found error2.msg // not found
- タプルに対してタプルの値を渡す
let error = (404, "not found") let (code, msg) = error code // 404 msg // not found
- タプルに渡ってくる複数の値のうち、使わないものがある場合は
_
で値を安全に破棄することができる
let error = (404, "not found") let (code, _) = error // "not found"は使わない code // 404 //msg // エラーになる
06 配列を使ってみよう
配列とは
- 関連性のある複数のデータを1つの変数名でまとめて管理したい場合に使う
// 配列 String型のデータを入れる var students: [String] = ["mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi", "nakayama"] // 値の取り出し/添字は0から students[0] // mayama students[1] // yasumoto // 値の変更 // ※ 「let students」 の場合は変更出来ない students[0] = "mizocchi" students // ["mizocchi", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi", "nakayama"]
便利な命令
- 要素の個数を数える
count
- 配列が空かどうかを調べる
isEmpty
- 末尾に要素を追加
append
- 任意の位置に入れる
insert
- 削除
removeAtIndex
- 末尾を削除
removeLast
// 配列 String型のデータを入れる var students: [String] = ["mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi", "nakayama"] students.count // 8 students.isEmpty // false // 末尾に追加 students.append("karisome") students // ["mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi", "nakayama", "karisome"] students.count // 9 // 任意の位置に追加 students.insert("mizuki", atIndex: 0) students // ["mizuki", "mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi", "nakayama", "karisome"] students.count // 10 // 任意の位置を削除 students.removeAtIndex(9) // karisome students // ["mizuki", "mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi", "nakayama"] students.count // 9 // 末尾を削除 // 削除すると削除されたものが返される let lastsamurai = students.removeLast() // nakayama students // ["mizuki", "mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi"] students.count // 8 lastsamurai // nakayama // --------------元に戻す students.removeAtIndex(0) // mizuki (´;ω;`) students // ["mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi"] students.count // 7 students.append(lastsamurai) students // ["mayama", "yasumoto", "hirota", "hosina", "matuno", "kashiwagi", "kobayasi", "nakayama"] students.count // 8
空の配列
String型
の場合はvar emptyArray = [String]()
07 辞書を使ってみよう
辞書とは
- キーと値のペアでデータを管理
// キーをString型、値をInt型で管理 var students: [String: Int] = [ "mayama": 3, "yasumoto": 5, "hirota": 6, "hoshina": 7, "matsuno": 9, "kashiwagi": 10, "kobayashi": 11, "nakayama": 12 ] // 値の取り出し(「Some」はOptionalという考え方。後で) students["yasumoto"] // {Some 5}
便利な命令
- ペアの数
count
- 空かどうか
isEmpty
- 値の追加
新しいキーで値を指定
- 削除
removeValueForKey
- 値の更新
updateValue
かキーを指定して値を更新
- キーと値の一覧を配列か何かで取りたい
xxx.keys
xxx.values
students.count // 8 students.isEmpty //false // 値の追加 students["suzuki"] = 8 // 順番は保証されてないみたい students // ["hoshina": 7, "mayama": 3, "kashiwagi": 10, "nakayama": 12, "matsuno": 9, "hirota": 6, "suzuki": 8, "kobayashi": 11, "yasumoto": 5] // 値の削除 students.removeValueForKey("suzuki") // {Some 8} students // ["hoshina": 7, "mayama": 3, "kashiwagi": 10, "nakayama": 12, "matsuno": 9, "hirota": 6, "kobayashi": 11, "yasumoto": 5] // 値の更新 // 元の値が返り値として返ってくる let number = students.updateValue(1, forKey: "mayama") // {Some 3} // キーを指定して値を更新することも出来る // students["mayama"] = 1 // {Some 1} students // ["hoshina": 7, "mayama": 1, "kashiwagi": 10, "nakayama": 12, "matsuno": 9, "hirota": 6, "kobayashi": 11, "yasumoto": 5] number // {Some 3} // キーの一覧を配列で取得 let keys = Array(students.keys) keys // ["hoshina", "mayama", "kashiwagi", "nakayama", "matsuno", "hirota", "kobayashi", "yasumoto"] // 値の一覧を配列で取得 let values = Array(students.values) values // [7, 1, 10, 12, 9, 6, 11, 5]
空の辞書型
- キーが
String型
で値がInt型
の場合->var emptyDictionary = [String: Int]()