LoginSignup
7
8

More than 5 years have passed since last update.

[iOS8] SwiftでDictionaryの値を削除する方法(removeValueForKey)

Posted at

Swiftで辞書の値を削除する方法をサンプルコードを利用して説明します。
値を削除したいだけの場合、keyにnilを代入することで削除可能です。

var dictionary    = ["one": 1, "two": 2, "three": 3]  // ["one": 1, "two": 2, "three": 3]
dictionary["one"] = nil                               // ["two": 2, "three": 3]

削除する値を取得したい場合は、removeValueForKeyを利用します。
let previousValue = dictionary.removeValueForKey("two") 
println(dictionary)      // ["three": 3]
println(previousValue)   // Optional(2)

 
7
8
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
7
8