Classes
The following classes are available globally.
-
Class defining shared resources for the QueryDecoder and QueryEncoder.
Usage Example:
See morelet date = Coder.defaultDateFormatter.date(from: "2017-10-31T16:15:56+0000")!
Declaration
Swift
public class Coder
-
Query Parameter Decoder decodes a
[String: String]
object to aDecodable
object instance. The decode function takes theDecodable
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 byQueryDecoder
is as follows:- Any Optional type (including
String?
) defaults tonil
- Non-optional
String
successfully decodes to""
- Non-optional
Bool
decodes tofalse
- All other non-optional types throw a decoding error
Declaration
Swift
public class QueryDecoder : Coder, Decoder, BodyDecoder
- Any Optional type (including
-
Query Parameter Encoder.
Encodes an
Encodable
object to a query parameter string, aURLQueryItemArray
, or to a[String: String]
dictionary. The encode function takes theEncodable
object to encode as the parameter.Usage Example:
See morelet 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 }
Declaration
Swift
public class QueryEncoder : Coder, Encoder, BodyEncoder