0
1

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 1 year has passed since last update.

SwiftでArrayとDictionaryのチートシート

Posted at

Array

宣言

// Arrayの定義
var array = [1, 2, 3, 4, 5]

// 要素の追加
array.append(6) // [1, 2, 3, 4, 5, 6]

// 指定した位置に要素を挿入
array.insert(0, at: 0) // [0, 1, 2, 3, 4, 5, 6]

// 要素の削除
array.remove(at: 0) // [1, 2, 3, 4, 5, 6]

// 指定した位置の要素のアクセス
let element = array[0] // 1

// 要素の変更
array[0] = 7 // [7, 2, 3, 4, 5, 6]

// 配列の長さを取得
let count = array.count // 6

Dictionary

宣言

// Dictionaryの定義
var dictionary = ["Apple": 1, "Banana": 2, "Cherry": 3]

// 要素の追加
dictionary["Durian"] = 4 // ["Apple": 1, "Banana": 2, "Cherry": 3, "Durian": 4]

// 指定したキーの要素のアクセス
let value = dictionary["Apple"] // Optional(1)

// 要素の削除
dictionary["Apple"] = nil // ["Banana": 2, "Cherry": 3, "Durian": 4]

// 要素の変更
dictionary["Banana"] = 5 // ["Banana": 5, "Cherry": 3, "Durian": 4]

// 辞書の長さを取得
let count = dictionary.count // 3

// NSDictionary を Dictionary にキャスト
let userDetails = userInfo["user_details"] as? [String: Any]
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?