6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftの配列とハッシュ(連想配列)

Posted at

今更ながら頭の整理のため。

Swift のバージョン

4.2.1

配列

// 宣言時に型を指定する場合。これは文字列の配列
var hoge: [String]

// こういう書き方もできる
var hoge: Array<String>

// 空で初期化
hoge = []

// 初期化時に代入する場合は型指定は無くても良い
var hoge = ["ほげ"]

// 最後に追加する場合はappend
hoge.append("ふが")

// 最初に追加する場合はinsert(at:)
hoge.insert("ふが", at: 0)

ハッシュ(連想配列)

// 宣言時に型を指定する場合。これはキーと値両方が文字列のハッシュ
var fuga: [String: String]

// こういう書き方もできる
var fuga: Dictionary<String, String>

// 空で初期化
fuga = [:]

// 初期化時に代入する場合は型指定は無くても良い
var fuga = ["ほげ": "ふが"]

// 追加/更新する場合
fuga["test"] = "test2"

複合

// これは文字列の配列を値に、文字列をキーに持つハッシュの配列
// 例えば
// [
//     "test": ["a", "b", "c"],
//     "test2": ["d", "e", "f"],
// ]
var test: [[String: [String]]]
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?