QueryDecoder

public class QueryDecoder : Coder, Decoder, BodyDecoder

Query Parameter Decoder decodes a [String: String] object to a Decodable object instance. The decode function takes the Decodable object as a parameter to decode the dictionary into.

Usage Example:

let dict = ["intField": "23", "stringField": "a string", "intArray": "1,2,3", "dateField": "2017-10-31T16:15:56+0000", "optionalDateField": "2017-10-31T16:15:56+0000", "nested": "{\"nestedIntField\":333,\"nestedStringField\":\"nested string\"}" ]

guard let query = try? QueryDecoder(dictionary: dict).decode(MyQuery.self) else {
    print("Failed to decode query to MyQuery Object")
    return
}

Decoding Empty Values:

When an HTML form is sent with an empty or unchecked field, the corresponding key/value pair is sent with an empty value (i.e. &key1=&key2=). The corresponding mapping to Swift types performed by QueryDecoder is as follows:

  • Any Optional type (including String?) defaults to nil
  • Non-optional String successfully decodes to ""
  • Non-optional Bool decodes to false
  • All other non-optional types throw a decoding error
  • The coding key path.

    Usage Example:

    let fieldName = Coder.getFieldName(from: codingPath)
    

    Declaration

    Swift

    public var codingPath: [CodingKey]
  • The coding user info key.

    Declaration

    Swift

    public var userInfo: [CodingUserInfoKey : Any]
  • A [String: String] dictionary.

    Declaration

    Swift

    public var dictionary: [String : String]
  • Initializer with an empty dictionary for decoding from Data.

    Declaration

    Swift

    public override init()
  • Initializer with a [String : String] dictionary.

    Declaration

    Swift

    public init(dictionary: [String : String])
  • Decode URL encoded data by mapping to its Decodable object representation.

    Usage Example:

    guard let query = try? QueryDecoder().decode(MyQuery.self, from queryData) else {
       print("Failed to decode query to MyQuery Object")
       return
    }
    

    Declaration

    Swift

    public func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable

    Parameters

    type

    The Decodable type to the Data will be decoded as.

    from

    The Data to be decoded as the Decodable type.

  • Decodes a [String: String] mapping to its Decodable object representation.

    Usage Example:

    guard let query = try? QueryDecoder(dictionary: expectedDict).decode(MyQuery.self) else {
        print("Failed to decode query to MyQuery Object")
        return
    }
    

    Declaration

    Swift

    public func decode<T>(_ type: T.Type) throws -> T where T : Decodable

    Parameters

    value

    The Decodable object to decode the dictionary into.

  • Returns a keyed decoding container based on the key type.

    Usage Example:

    decoder.container(keyedBy: keyType)
    

    Declaration

    Swift

    public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey
  • Returns an unkeyed decoding container.

    Usage Example:

    decoder.unkeyedContainer()
    

    Declaration

    Swift

    public func unkeyedContainer() throws -> UnkeyedDecodingContainer
  • Returns a single value decoding container based on the key type.

    Usage Example:

    decoder.singleValueContainer()
    

    Declaration

    Swift

    public func singleValueContainer() throws -> SingleValueDecodingContainer