Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Dictionary

pNre edited this page Jun 18, 2014 · 18 revisions

Contents

Instance Methods

Iteration

each

  • each (eachFunction each: (KeyType, ValueType) -> ())

Calls eachFunction for each (key, value) couple in self.

Examples

Without indexes
let dictionary = ["A": 1, "B": 2, "C": 3]
dictionary.each { key, value in println(key, value) }
/* Prints → */
// (A, 1)
// (B, 2)
// (C, 3)

Items accessing

has

  • has (key: KeyType) -> Bool

Returns true if key is in self, false otherwise.

Examples

Without indexes
let dictionary = ["A": 1, "B": 2, "C": 3]
dictionary.has("A")
// → true

pick, at

  • pick (keys: KeyType[]) -> Dictionary
  • pick (keys: KeyType...) -> Dictionary
  • at (keys: KeyType...) -> Dictionary

Return a copy of self, containing the couples (key, value) for the whitelisted keys.

Examples

Without indexes
let dictionary = ["A": 1, "B": 2, "C": 3, "D": 4]
dictionary.pick("A", "C")
// → ["A": 1, "C": 3]

Items testing

any

  • any (test: (KeyType, ValueType) -> (Bool)) -> Bool

Executes test on each (key, value) in self and returns true if test returns true for any couple, false otherwise.

Example

let dictionary = ["A": 1, "B": 2, "C": 3, "D": 4]

dictionary.any { key, value -> Bool in value < 0 }
// → false

dictionary.any { key, value -> Bool in key == "A" }
// → true

all

  • all (test: (KeyType, ValueType) -> (Bool)) -> Bool

Executes test on each (key, value) in self and returns true if test returns true for each couple, false otherwise.

Example

let dictionary = ["A": 1, "B": 2, "C": 3, "D": 4]

dictionary.all { key, value -> Bool in value > 0 }
// → true

dictionary.all { key, value -> Bool in key == "A" }
// → false

##Sets ###difference

  • difference <V: Equatable> (dictionaries: Dictionary<KeyType, V>...) -> Dictionary<KeyType, V>

Computes the difference between self and the input dictionaries, by keys.

Example

let dictionary1 = [ "A": 1, "B": 2, "C": 3 ]
let dictionary2 = [ "A": 1 ]

let diff1 = dictionary1.difference(dictionary2)
// → [C: 3, B: 2]
Clone this wiki locally