Classes

The following classes are available globally.

  • Class defining shared resources for the QueryDecoder and QueryEncoder.

    Usage Example:

    let date = Coder.defaultDateFormatter.date(from: "2017-10-31T16:15:56+0000")!
    
    See more

    Declaration

    Swift

    public class Coder
  • 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
    See more

    Declaration

    Swift

    public class QueryDecoder : Coder, Decoder, BodyDecoder
  • 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
    }
    
    See more

    Declaration

    Swift

    public class QueryEncoder : Coder, Encoder, BodyEncoder