QueryEncoder

public class QueryEncoder : Coder, Encoder, BodyEncoder

Query Parameter Encoder.

Encodes an Encodable object to a query parameter string, a URLQueryItemArray, or to a [String: String] dictionary. The encode function takes the Encodable object to encode as the parameter.

Usage Example:

let date = Coder().dateFormatter.date(from: "2017-10-31T16:15:56+0000")
let query = MyQuery(intField: -1, optionalIntField: 282, stringField: "a string", intArray: [1, -1, 3], dateField: date, optionalDateField: date, nested: Nested(nestedIntField: 333, nestedStringField: "nested string"))

guard let myQueryDict: [String: String] = try? QueryEncoder().encode(query) else {
    print("Failed to encode query to [String: String]")
    return
}
  • 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]
  • Initializer for the dictionary, which initializes an empty [String: String] dictionary.

    Declaration

    Swift

    public override init()
  • Encodes an Encodable object to a query parameter string.

    Usage Example:

    guard let myQueryStr: String = try? QueryEncoder().encode(query) else {
        print("Failed to encode query to String")
        return
    }
    

    Declaration

    Swift

    public func encode<T>(_ value: T) throws -> String where T : Encodable

    Parameters

    value

    The Encodable object to encode to its String representation.

  • Encodes an Encodable object to Data.

    Usage Example:

    guard let myQueryStr: Data = try? QueryEncoder().encode(query) else {
       print("Failed to encode query to Data")
       return
    }
    

    Declaration

    Swift

    public func encode<T>(_ value: T) throws -> Data where T : Encodable

    Parameters

    value

    The Encodable object to encode to its Data representation.

  • Encodes an Encodable object to a URLQueryItem array.

    Usage Example:

    guard let myQueryArray: [URLQueryItem] = try? QueryEncoder().encode(query) else {
       print("Failed to encode query to [URLQueryItem]")
       return
    }
    

    Declaration

    Swift

    public func encode<T>(_ value: T) throws -> [URLQueryItem] where T : Encodable

    Parameters

    value

    The Encodable object to encode to its [URLQueryItem] representation.

  • Encodes an Encodable object to a [String: String] dictionary.

    Usage Example:

    guard let myQueryDict: [String: String] = try? QueryEncoder().encode(query) else {
        print("Failed to encode query to [String: String]")
        return
    }
    

    Declaration

    Swift

    public func encode<T>(_ value: T) throws -> [String : String] where T : Encodable

    Parameters

    value

    The Encodable object to encode to its [String: String] representation.

  • Encodes an Encodable object to a String -> String dictionary

    Declaration

    Swift

    public func encode<T>(_ value: T) throws -> [String : Any] where T : Encodable
  • Returns a keyed encoding container based on the key type.

    Usage Example:

    encoder.container(keyedBy: keyType)
    

    Declaration

    Swift

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

    Usage Example:

    encoder.unkeyedContainer()
    

    Declaration

    Swift

    public func unkeyedContainer() -> UnkeyedEncodingContainer
  • Returns an single value encoding container based on the key type.

    Usage Example:

    encoder.singleValueContainer()
    

    Declaration

    Swift

    public func singleValueContainer() -> SingleValueEncodingContainer