Dictionary

public extension Dictionary
public extension Dictionary where Value: Equatable
public extension Dictionary where Key: StringProtocol
  • SwifterSwift: Creates a Dictionary from a given sequence grouped by a given key path.

    Declaration

    Swift

    init<S>(grouping sequence: S, by keyPath: KeyPath<S.Element, Key>) where Value == [S.Element], S : Sequence

    Parameters

    sequence

    Sequence being grouped

    keypath

    The key path to group by.

  • 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

    func has(key: Key) -> Bool

    Parameters

    key

    key to search for

    Return Value

    true if key exists in dictionary.

  • SwifterSwift: Remove all keys contained in the keys parameter from 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

    mutating mutating func removeAll<S>(keys: S) where Key == S.Element, S : Sequence

    Parameters

    keys

    keys to be removed

  • SwifterSwift: Remove a value for a random key from the dictionary.

    Declaration

    Swift

    @discardableResult
    mutating mutating func removeValueForRandomKey() -> Value?
  • SwifterSwift: JSON Data from dictionary.

    Declaration

    Swift

    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

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

    Parameters

    prettify

    set true to prettify string (default is false).

    Return Value

    optional JSON String (if applicable).

  • SwifterSwift: Returns a dictionary containing the results of mapping the given closure over the sequence’s elements.

    Declaration

    Swift

    func mapKeysAndValues<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)) rethrows -> [K : V] where K : Hashable

    Parameters

    transform

    A mapping closure. transform accepts an element of this sequence as its parameter and returns a transformed value of the same or of a different type.

    Return Value

    A dictionary containing the transformed elements of this sequence.

  • SwifterSwift: Returns a dictionary containing the non-nil results of calling the given transformation with each element of this sequence.

    Complexity

    O(m + n), where m is the length of this sequence and n is the length of the result.

    Declaration

    Swift

    func compactMapKeysAndValues<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)?) rethrows -> [K : V] where K : Hashable

    Parameters

    transform

    A closure that accepts an element of this sequence as its argument and returns an optional value.

    Return Value

    A dictionary of the non-nil results of calling transform with each element of the sequence.

Subscripts

  • SwifterSwift: Deep fetch or set a value from nested dictionaries.

       var dict =  ["key": ["key1": ["key2": "value"]]]
       dict[path: ["key", "key1", "key2"]] = "newValue"
       dict[path: ["key", "key1", "key2"]] -> "newValue"
    

    Note

    Value fetching is iterative, while setting is recursive.

    Complexity

    O(N), N being the length of the path passed in.

    Declaration

    Swift

    subscript(path path: [Key]) -> Any? { get set }

    Parameters

    path

    An array of keys to the desired value.

    Return Value

    The value for the key-path passed in. nil if no value is found.

Operators

  • 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

    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

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

    Parameters

    lhs

    dictionary

    rhs

    dictionary

  • SwifterSwift: Remove keys contained in the sequence 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

    static func - <S>(lhs: [Key : Value], keys: S) -> [Key : Value] where Key == S.Element, S : Sequence

    Parameters

    lhs

    dictionary

    rhs

    array with the keys to be removed.

    Return Value

    a new dictionary with keys removed.

  • SwifterSwift: Remove keys contained in the sequence 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

    static func -= <S>(lhs: inout [Key : Value], keys: S) where Key == S.Element, S : Sequence

    Parameters

    lhs

    dictionary

    rhs

    array with the keys to be removed.

Available where Value: Equatable

  • SwifterSwift: Returns an array of all keys that have the given value in dictionary.

       let dict = ["key1": "value1", "key2": "value1", "key3": "value2"]
       dict.keys(forValue: "value1") -> ["key1", "key2"]
       dict.keys(forValue: "value2") -> ["key3"]
       dict.keys(forValue: "value3") -> []
    

    Declaration

    Swift

    func keys(forValue value: Value) -> [Key]

    Parameters

    value

    Value for which keys are to be fetched.

    Return Value

    An array containing keys that have the given value.

Available where Key: StringProtocol

  • SwifterSwift: Lowercase all keys in dictionary.

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

    Declaration

    Swift

    mutating mutating func lowercaseAllKeys()