Hash

public typealias Hash<K, V> = Dictionary<K, V>
  • Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.

    let h1 = ["a": 100, "b": 200]
    h1.isAny { (key, _) in key == "a" }     #=> true
    h1.isAny { (key, _) in key == "c" }     #=> false
    

    Declaration

    Swift

    func isAny(closure: (Key, Value) -> Bool) -> Bool

    Parameters

    closure

    A closure accepts key and value pair from the hash.

    Return Value

    A bool value.

  • Returns true if the given key is present in hsh.

    let hash = ["a": 100, "b": 200]
    hash.has(key: "a")      #=> true
    hash.has(key: "c")      #=> false
    

    Declaration

    Swift

    func has(key: Key) -> Bool

    Parameters

    key

    A string represent key in hsh.

    Return Value

    A bool value.

  • Returns true if the given key is present in hsh. An alias to isMember(key:) methods.、

    let hash = ["a": 100, "b": 200]
    hash.isMember("a")      #=> true
    hash.isMember("c")      #=> false
    

    Declaration

    Swift

    func isMember(_ key: Key) -> Bool

    Parameters

    key

    A string represent key in hsh.

    Return Value

    A bool value.

  • Returns true if the given value is present in hsh.

    let hash = ["a": 100, "b": 200]
    hash.has(value: 100)        #=> true
    hash.has(value: 2000)       #=> false
    

    Declaration

    Swift

    func has(value: Value) -> Bool

    Parameters

    key

    A string represent value in hsh.

    Return Value

    A bool value.

  • Calls block once for each key in hsh, passing the key-value pair as parameters. An alias to Sequence#each(closure:) method.

    let hash = ["a": 100, "b": 200]
    var result: [String: Int] = [:]
    hash.eachPair {
        result[$0] = $1 + 100
    })          #=> ["a": 100, "b": 200]
    
    result      #=> ["a": 200, "b": 300]
    

    Declaration

    Swift

    func eachPair(closure: (Key, Value) -> Void) -> Hash<Key, Value>

    Parameters

    closure

    An closure accepts key-value pair as parameters.

    Return Value

    Self.

  • Calls block once for each key in hsh, passing the key as parameters.

    let hash = ["a": 100, "b": 200]
    var result: [String] = []
    hash.eachKey {
        result.append($0)
    })          #=> ["a", "b"]
    
    result      #=> ["a": 100, "b": 200]
    

    Declaration

    Swift

    func eachKey(closure: (Key) -> Void) -> Hash<Key, Value>

    Parameters

    closure

    An closure accepts key as parameters.

    Return Value

    Self.

  • Calls block once for each key in hsh, passing the value as parameters.

    let hash = ["a": 100, "b": 200]
    var result: [Int] = []
    hash.eachValue {
        result.append($0)
    })          #=> [100, 200]
    
    result      #=> ["a": 100, "b": 200]
    

    Declaration

    Swift

    func eachValue(closure: (Value) -> Void) -> Hash<Key, Value>

    Parameters

    closure

    An closure accepts value as parameters.

    Return Value

    Self.

  • Returns an array of optinal value with given keys.

    let hash = ["cat": "feline", "dog": "canine", "cow": "bovine"]
    hash.values(at: "cat", "dog")           #=> ["feline", "canine"]
    hash.values(at: "catcat", "dog")        #=> [nil, "canine"]
    

    Declaration

    Swift

    func values(at keys: Key...) -> [Value?]

    Parameters

    keys

    An array of keys.

    Return Value

    An optioanl value of array.

  • Returns an array of optinal value with given keys.

    let hash = ["cat": "feline", "dog": "canine", "cow": "bovine"]
    hash.values(at: ["cat", "dog"])         #=> ["feline", "canine"]
    hash.values(at: ["catcat", "dog"])      #=> [nil, "canine"]
    

    Declaration

    Swift

    func values(at keys: [Key]) -> [Value?]

    Parameters

    keys

    An array of keys.

    Return Value

    An optioanl value of array.

  • Searches through the hash with key using subscript. Returns the key-value tuple or nil if no match is found.

    let hash = [1: "one", 2: "two", 3: "three"]
    hash.assoc(1)       #=> (1, "one")
    hash.assoc(42)      #=> nil
    

    Declaration

    Swift

    func assoc(_ key: Key) -> (Key, Value)?

    Parameters

    key

    An key of type Key

    Return Value

    A key-value tuple or nil.

  • Returns a value from the hash for the given key.

    let hash = ["a": 100, "b": 200]
    hash.fetch("a")     #=> 100
    

    If the key can’t be found, there are several options: With no other arguments, it will return nil;

    hash.fetch("z")     #=> nil
    

    if default is given, then that will be returned;

    hash.fetch("z", 500)        #=> 500
    

    Declaration

    Swift

    func fetch(_ key: Key, _ default: Value) -> Value

    Parameters

    key

    A key.

    default

    Default value if key can’t be found.

    Return Value

    A value or nil

  • Returns a value from the hash for the given key.

    let hash = ["a": 100, "b": 200]
    hash.fetch("a")     #=> 100
    

    If the key can’t be found, there are several options: if the optional code block is specified, then that will be run and its result returned.

    hash.fetch("z", closure: { _ in
        return 1000
    })      #=> 1000
    

    Declaration

    Swift

    func fetch(_ key: Key, closure: ((Key) -> Value)? = nil) -> Value?

    Parameters

    key

    A key.

    closure

    A code block will executes on the key if not found.

    Return Value

    A value or nil

  • Returns an array containing the values associated with the given keys or nil when one of keys can’t be found. Also see Hash#values(at:) and Hash#fetch(key:).

    let hash = ["cat": "feline", "dog": "canine", "cow": "bovine"]
    hash.fetchValues("cow", "cat")!     #=> ["bovine", "feline"]
    hash.fetchValues("cow", "bird")     #=> nil
    

    Declaration

    Swift

    func fetchValues(_ keys: Key...) -> [Value]?

    Parameters

    keys

    An array of keys.

    Return Value

    An array of values or nil.

  • Returns an array containing the values associated with the given keys or nil when one of keys can’t be found. Also see Hash#values(at:) and Hash#fetch(key:).

    let hash = ["cat": "feline", "dog": "canine", "cow": "bovine"]
    hash.fetchValues("cow", "bird") { key in
        return key.upcase
    }       #=> ["bovine", "BIRD"]
    

    Declaration

    Swift

    func fetchValues(_ keys: Key..., closure: ((Key) -> Value)) -> [Value]

    Parameters

    keys

    An array of keys.

    closure

    A closure executs each time value is not found.

    Return Value

    An array of values.

  • Returns a hash that includes everything except given keys.

    let hash = [1: "one", 2: "two", 3: "three"]
    hash.except(1, 2)       #=> [3: "three"]
    hash                    #=> [1: "one", 2: "two", 3: "three"]
    

    Declaration

    Swift

    func except(_ keys: Key...) -> [Key: Value]

    Parameters

    keys

    An array of keys.

    Return Value

    An new hash without passing keys.

  • Returns a hash that includes everything except given keys.

    let hash = [1: "one", 2: "two", 3: "three"]
    hash.except(1, 2)       #=> [3: "three"]
    hash                    #=> [1: "one", 2: "two", 3: "three"]
    

    Declaration

    Swift

    func except(_ keys: [Key]) -> [Key: Value]

    Parameters

    keys

    An array of keys.

    Return Value

    An new hash without passing keys.

  • Removes the given keys from hash and returns it.

    var hash = [1: "one", 2: "two", 3: "three"]
    hash.excepted(1, 2)     #=> [3: "three"]
    hash                    #=> [3: "three"]
    

    Declaration

    Swift

    mutating func excepted(_ keys: Key...) -> [Key: Value]

    Parameters

    keys

    An array of keys.

    Return Value

    Key/value pairs with given keys.

  • Removes the given keys from hash and returns it.

    var hash = [1: "one", 2: "two", 3: "three"]
    hash.excepted(1, 2)     #=> [3: "three"]
    hash                    #=> [3: "three"]
    

    Declaration

    Swift

    mutating func excepted(_ keys: [Key]) -> [Key: Value]

    Parameters

    keys

    An array of keys.

    Return Value

    Key/value pairs with given keys.

  • Removes and returns the key/value pairs matching the given keys.

    var hash1 = [1: "one", 2: "two", 3: "three"]
    hash1.extract(1, 2)     #=> [1: "one", 2: "two"]
    hash1                   #=> [3: "three"]
    
    var hash2 = [1: "one", 2: "two", 3: "three"]
    hash2.extract(1, 4)     #=> [1: "one"]
    hash2                   #=> [2: "two", 3: "three"]
    

    Declaration

    Swift

    mutating func extract(_ keys: Key...) -> [Key: Value]

    Parameters

    keys

    An array of keys.

    Return Value

    Key-value pairs matching the given keys

  • Removes and returns the key/value pairs matching the given keys.

    var hash1 = [1: "one", 2: "two", 3: "three"]
    hash1.extract(1, 2)     #=> [1: "one", 2: "two"]
    hash1                   #=> [3: "three"]
    
    var hash2 = [1: "one", 2: "two", 3: "three"]
    hash2.extract(1, 4)     #=> [1: "one"]
    hash2                   #=> [2: "two", 3: "three"]
    

    Declaration

    Swift

    mutating func extract(_ keys: [Key]) -> [Key: Value]

    Parameters

    keys

    An array of keys.

    Return Value

    Key-value pairs matching the given keys

  • Searches through the hash with value using subscript. Returns the key-value tuple or nil if no match is found.

    let hash = [1: "one", 2: "two", 3: "three"]
    hash.rassoc("one")          #=> (1, "one")
    hash.rassoc("forty-two")    #=> nil
    

    Declaration

    Swift

    func rassoc(_ value: Value) -> (Key, Value)?

    Parameters

    key

    An key of type Key

    Return Value

    A key-value tuple or nil.

  • Returns a new hash containing the contents of otherHash and the contents of hsh. If no block is specified, the value for entries with duplicate keys will be that of otherHash.

    let h1 = ["a": 100, "b": 200]
    let h2 = ["b": 254, "c": 300]
    
    h1.merge(h2)        #=> ["a": 100, "b": 254, "c": 300]))
    

    Otherwise the value for each duplicate key is determined by calling the block with the key, its value in hsh and its value in otherHash.

    h1.merge(h2) { (key, oldval, newval) in
        newval - oldval
    }       #=> ["a": 100, "b": 54,  "c": 300]
    
    h1      #=> ["a": 100, "b": 200]
    

    Declaration

    Swift

    func merge(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value>

    Parameters

    otherHash

    Another hash instance.

    closure

    A closure returns a new value if duplicate happens.

    Return Value

    A new hash containing the contents of both hash.

  • An alias to Hash#merge(otherHash:) methods.

    Declaration

    Swift

    func update(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value>

    Parameters

    otherHash

    Another hash instance.

    closure

    A closure returns a new value if duplicate happens.

    Return Value

    A new hash containing the contents of both hash.

  • A mutating version of Hash#merge(otherHash:closure:)

    let h1 = ["a": 100, "b": 200]
    let h2 = ["b": 254, "c": 300]
    
    h1.merge(h2)        #=> ["a": 100, "b": 254, "c": 300]))
    h1                  #=> ["a": 100, "b": 254, "c": 300]))
    

    Declaration

    Swift

    mutating func merged(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value>

    Parameters

    otherHash

    Another hash instance.

    closure

    A closure returns a new value if duplicate happens.

    Return Value

    Self

  • An alias to Hash#merged(otherHash:) methods.

    Declaration

    Swift

    mutating func updated(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value>

    Parameters

    otherHash

    Another hash instance.

    closure

    A closure returns a new value if duplicate happens.

    Return Value

    Self

  • Removes all key-value pairs from hsh.

    var hash = ["a": 100]
    hash.clear()        #=> [:]
    hash                #=> [:]
    

    Declaration

    Swift

    mutating func clear() -> Hash<Key, Value>

    Return Value

    Self with empty hash.

  • An alias to Hash#removeValue(forKey:).

    var hash = ["a": 100, "b": 200]
    hash.delete("a")        #=> 100
    hash                    #=> ["b": 200]
    

    Declaration

    Swift

    mutating func delete(_ key: Key) -> Value?

    Parameters

    key

    A key of hash.

    Return Value

    Corresponding value or nil.

  • Replaces the contents of hsh with the contents of otherHash.

    var hash = ["a": 100, "b": 200]
    hash.replace(["c": 300])        #=> ["c": 300]
    hash                            #=> ["c": 300]
    

    Declaration

    Swift

    mutating func replace(_ otherHash: Hash<Key, Value>) -> Hash<Key, Value>

    Parameters

    otherHash

    Another hash instance.

    Return Value

    Self

  • Associates the value given by value with the key given by key.

    var hash = ["a": 4]
    hash.store("b", 5)      #=> 5
    hash                    #=> ["a": 4, "b": 5]
    

    Declaration

    Swift

    mutating func store(_ key: Key, _ value: Value) -> Value

    Parameters

    key

    A key

    value

    A value

    Return Value

    The passing value

  • Removes a key-value pair from hsh and returns it as the two-item array ( key, value ), or nil if the hash is empty.

    var hash = ["a": 4]
    hash.shift()        #=> ("b", 5)
    hash.shift()        #=> nil
    

    Declaration

    Swift

    mutating func shift() -> (Key, Value)?

    Return Value

    A key-value pair

  • Return a new with the results of running block once for every value. This method does not change the keys.

    let hash = ["a": 1, "b": 2, "c": 3]
    hash.transformValues { $0 * $0 + 1 }    #=> ["a": 2, "b": 5, "c": 10]
    hash.transformValues { $0.to_s }        #=> ["a": "1", "b": "2", "c": "3"]
    

    Declaration

    Swift

    func transformValues<T>(_ closure: @escaping (Value) -> T) -> [Key: T]

    Parameters

    closure

    An closure accepts a value return an new value.

    Return Value

    An new hash with key-value pair

  • Return a new with the results of running block once for every value. This method does not change the keys. The mutates version of Hash#transformedValues(closure:) can only accepts a closure with (Value) -> Value type.

    var hash = ["a": 1, "b": 2, "c": 3]
    hash.transformedValues { 
        $0 * $0 + 1 
    }       #=> ["a": 2, "b": 5, "c": 10]
    hash    #=> ["a": 2, "b": 5, "c": 10]
    

    Declaration

    Swift

    mutating func transformedValues(_ closure: @escaping (Value) -> Value) -> [Key: Value]

    Parameters

    closure

    An closure accepts a value return an new value.

    Return Value

    Self

  • Returns a new hash created by using hsh’s values as keys, and the keys as values.

    let hash1 = ["a": 100, "b": 200]
    hash1.invert            #=> [100: "a", 200: "b"]
    
    let hash2 = ["cat": "feline", "dog": "canine", "cow": "bovine"]
    hash2.invert.invert     #=> ["cat": "feline", "dog": "canine", "cow": "bovine"]
    

    If a key with the same value already exists in the hsh, then the last one defined will be used, the earlier value(s) will be discarded.

    let hash3 = ["cat": 1, "dog": 1]
    hash3.invert    #=> [1: "dog"]
    

    Declaration

    Swift

    var invert: [Value: Key]