ADSPONDecoder

public class ADSPONDecoder: Decoder

Decodes a Codable or Decodable class from a ADRecord read from a SQLite database using a ADSPONiteProvider. The result is an instance of the class with the properties set from the database record. This decoder will automatically handle URLs and Enums (if the Enum is value based and also marked Codable or Decodable).

Example:

import ActionUtilities
import ActionData

class Category: ADDataTable {

    enum CategoryType: String, Codable {
        case local
        case web
    }

    static var tableName = "Categories"
    static var primaryKey = "id"
    static var primaryKeyType: ADDataTableKeyType = .computedInt

    var id = 0
    var added = Date()
    var name = ""
    var description = ""
    var enabled = true
    var highlightColor = UIColor.white.toHex()
    var type: CategoryType = .local
    var icon: Data = UIImage().toData()

    required init() {

    }
}

let encoder = ADSPONEncoder()
let category = Category()
let data = try encoder.encode(category)

let decoder = ADSPONDecoder()
let category2 = try decoder.decode(Category.self, from: data)

Remark

To retrieve UIColors in the record use the String.uiColor extension property and to retrieve UIImages use the String.uiImage extension property.
  • Checks to see if a sqlObject is stored in the given data stream and returns it if it is.

    Declaration

    Swift

    public static func sqlObject(in data: Any) -> Any?

    Parameters

    data

    The data to check for a ADRecord or ADRecord array.

    Return Value

    The ADRecord or ADRecord array if found, else nil.

  • Shared formatter used to encode a Date as an ISO-8601-formatted string (in RFC 3339 format).

    Declaration

    Swift

    public static var iso8601Formatter: ISO8601DateFormatter =
  • The path to the current value that is being decoded.

    Declaration

    Swift

    public var codingPath: [CodingKey] = []
  • User spsecific information that can be used when decoding an item.

    Declaration

    Swift

    public var userInfo: [CodingUserInfoKey : Any] = [:]
  • The strategy used to decode Date properties. The default is rawDate which allow the ADSPONiteProvider to handle the date directly.

    Declaration

    Swift

    public var dateDecodingStrategy: DateDecodingStrategy = .rawDate
  • The strategy used to encode Data or NSData properties. The default is rawData which allow the ADSPONiteProvider to handle the data directly.

    Declaration

    Swift

    public var dataDecodingStrategy: DataDecodingStrategy = .rawData
  • Creates a new instance of the decoder.

    • dateDecodingStrategy: The strategy used to decode Date properties. The default is rawDate which allow the ADSPONiteProvider to handle the date directly.
    • dataDecodingStrategy: The strategy used to decode Data or NSData properties. The default is rawData which allow the ADSPONiteProvider to handle the data directly.

    Declaration

    Swift

    public init(dateDecodingStrategy: DateDecodingStrategy = .rawDate, dataDecodingStrategy: DataDecodingStrategy = .rawData)
  • Decodes a Codable or Decodable class from a ADRecord read from a SQLite database using a ADSPONiteProvider. The result is an instance of the class with the properties set from the database record. This decoder will automatically handle URLs and Enums (if the Enum is value based and also marked Codable or Decodable). For example:

    enum SwitchState: String, Codable {
        case on
        case off
    }
    

    Example Usage

    let record = ADSPONiteProvider.shared.query("SELECT * FROM TASKS WHERE ID=1")
    let decoder = ADSPONDecoder()
    let task = decoder.decode(Task, from: record)
    

    Remark

    To retrieve UIColors in the record use the String.uiColor extension property and to retrieve UIImages use the String.uiImage extension property.

    Declaration

    Swift

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

    Parameters

    type

    The type of class to decode the data to.

    data

    The SPON encoded string to decode.

    Return Value

    The data decoded to one (or more) instances of the given class.

  • Returns the keyed decoding container for the given key type.

    Declaration

    Swift

    public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey

    Parameters

    type

    The key of key to return to container for.

    Return Value

    A KeyedDecodingContainer for the given key.

  • Returns an unkeyed decoding container.

    Declaration

    Swift

    public func unkeyedContainer() throws -> UnkeyedDecodingContainer

    Return Value

    A UnkeyedDecodingContainer.

  • Returns a single value decoding container.

    Declaration

    Swift

    public func singleValueContainer() throws -> SingleValueDecodingContainer

    Return Value

    A SingleValueDecodingContainer.