Dictionary

struct Dictionary<Key, Value> where Key : Hashable
  • SwifterSwift: Check if key exists in dictionary.

    let dict: [String : Any] = ["testKey": "testValue", "testArrayKey": [1, 2, 3, 4, 5]]
    dict.has(key: "testKey") -> true
    dict.has(key: "anotherKey") -> false
    

    Declaration

    Swift

    public func has(key: Key) -> Bool

    Parameters

    key

    key to search for

    Return Value

    true if key exists in dictionary.

  • SwifterSwift: Remove all keys of the dictionary.

    var dict : [String : String] = ["key1" : "value1", "key2" : "value2", "key3" : "value3"]
    dict.removeAll(keys: ["key1", "key2"])
    dict.keys.contains("key3") -> true
    dict.keys.contains("key1") -> false
    dict.keys.contains("key2") -> false
    

    Declaration

    Swift

    public mutating func removeAll(keys: [Key])

    Parameters

    keys

    keys to be removed

  • SwifterSwift: JSON Data from dictionary.

    Declaration

    Swift

    public func jsonData(prettify: Bool = false) -> Data?

    Parameters

    prettify

    set true to prettify data (default is false).

    Return Value

    optional JSON Data (if applicable).

  • SwifterSwift: JSON String from dictionary.

    dict.jsonString() -> "{"testKey":"testValue","testArrayKey":[1,2,3,4,5]}"
    
    dict.jsonString(prettify: true)
    /*
    returns the following string:
    
    "{
    "testKey" : "testValue",
    "testArrayKey" : [
        1,
        2,
        3,
        4,
        5
    ]
    }"
    
    */
    

    Declaration

    Swift

    public func jsonString(prettify: Bool = false) -> String?

    Parameters

    prettify

    set true to prettify string (default is false).

    Return Value

    optional JSON String (if applicable).

  • SwifterSwift: Count dictionary entries that where function returns true.

    Declaration

    Swift

    public func count(where condition: @escaping ((key: Key, value: Value)) throws -> Bool) rethrows -> Int

    Parameters

    where

    condition to evaluate each tuple entry against.

    Return Value

    Count of entries that matches the where clousure.

  • SwifterSwift: Lowercase all keys in dictionary.

       var dict = ["tEstKeY": "value"]
       dict.lowercaseAllKeys()
       print(dict) // prints "["testkey": "value"]"
    

    Declaration

    Swift

    public mutating func lowercaseAllKeys()
  • SwifterSwift: Merge the keys/values of two dictionaries.

    let dict : [String : String] = ["key1" : "value1"]
    let dict2 : [String : String] = ["key2" : "value2"]
    let result = dict + dict2
    result["key1"] -> "value1"
    result["key2"] -> "value2"
    

    Declaration

    Swift

    public static func + (lhs: [Key: Value], rhs: [Key: Value]) -> [Key: Value]

    Parameters

    lhs

    dictionary

    rhs

    dictionary

    Return Value

    An dictionary with keys and values from both.

  • SwifterSwift: Append the keys and values from the second dictionary into the first one.

    var dict : [String : String] = ["key1" : "value1"]
    let dict2 : [String : String] = ["key2" : "value2"]
    dict += dict2
    dict["key1"] -> "value1"
    dict["key2"] -> "value2"
    

    Declaration

    Swift

    public static func += (lhs: inout [Key: Value], rhs: [Key: Value])

    Parameters

    lhs

    dictionary

    rhs

    dictionary

  • SwifterSwift: Remove contained in the array from the dictionary

    let dict : [String : String] = ["key1" : "value1", "key2" : "value2", "key3" : "value3"]
    let result = dict-["key1", "key2"]
    result.keys.contains("key3") -> true
    result.keys.contains("key1") -> false
    result.keys.contains("key2") -> false
    

    Declaration

    Swift

    public static func - (lhs: [Key: Value], keys: [Key]) -> [Key: Value]

    Parameters

    lhs

    dictionary

    rhs

    array with the keys to be removed.

    Return Value

    a new dictionary with keys removed.

  • SwifterSwift: Remove contained in the array from the dictionary

    var dict : [String : String] = ["key1" : "value1", "key2" : "value2", "key3" : "value3"]
    dict-=["key1", "key2"]
    dict.keys.contains("key3") -> true
    dict.keys.contains("key1") -> false
    dict.keys.contains("key2") -> false
    

    Declaration

    Swift

    public static func -= (lhs: inout [Key: Value], keys: [Key])

    Parameters

    lhs

    dictionary

    rhs

    array with the keys to be removed.