ADSQLEncoder
public class ADSQLEncoder: Encoder
Encodes a Codable
or Encodable
class into a ADRecord
that can be written into a SQLite database using a ADSQLiteProvider
. The result is a dictionary of key/value pairs representing the data currently stored in the class. This encoder will automatically handle URLs
and Enums
(if the Enum is value based and also marked Codable
or Encodable
).
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 = ADSQLEncoder()
let category = Category()
let data = try encoder.encode(category)
Remark
To storeUIColors
in the record use the toHex()
extension method and to store UIImages
use the toData()
extension method.
-
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 element currently being encoded.
Declaration
Swift
public var codingPath: [CodingKey] = []
-
User specific, additional information to be encoded in the output.
Declaration
Swift
public var userInfo: [CodingUserInfoKey : Any] = [:]
-
The strategy used to encode
Date
properties. The default israwDate
which allow theADSQLiteProvider
to handle the date directly.Declaration
Swift
public var dateEncodingStrategy: DateEncodingStrategy = .rawDate
-
The strategy used to encode
Data
orNSData
properties. The default israwData
which allow theADSQLiteProvider
to handle the data directly.Declaration
Swift
public var dataEncodingStrategy: DataEncodingStrategy = .rawData
-
Creates a new instance of the encoder.
Declaration
Swift
public init(dateEncodingStrategy: DateEncodingStrategy = .rawDate, dataEncodingStrategy: DataEncodingStrategy = .rawData)
Parameters
dateEncodingStrategy
The strategy used to encode
Date
properties. The default israwDate
which allow theADSQLiteProvider
to handle the date directly.dataEncodingStrategy
The strategy used to encode
Data
orNSData
properties. The default israwData
which allow theADSQLiteProvider
to handle the data directly.
-
Encodes a
Codable
orEncodable
class into aADRecord
that can be written into a SQLite database using aADSQLiteProvider
. The result is a dictionary of key/value pairs representing the data currently stored in the class. This encoder will automatically handleURLs
andEnums
(if the Enum is value based and also markedCodable
orEncodable
). For example:enum SwitchState: String, Codable { case on case off }
Example Usage
let object = MySQLRecordClass() let encoder = ADSQLEncoder() let record = encoder.encode(object)
Remark
To storeUIColors
in the record use thetoHex()
extension method and to storeUIImages
use thetoData()
extension method.Declaration
Swift
public func encode<T:Encodable>(_ value: T) throws -> Any
Parameters
value
The object to encode.
Return Value
A dictionary of key/value pairs representing the data currently stored in the class.
-
Returns a key/value encoding container for the given key type.
Declaration
Swift
public func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey
Parameters
type
The type of key to create an encoding container for.
Return Value
A
KeyedEncodingContainer
instance for the given key. -
Returns an unkeyed encodign container.
Declaration
Swift
public func unkeyedContainer() -> UnkeyedEncodingContainer
Return Value
A
UnkeyedEncodingContainer
instance. -
Returns a single value encoding container.
Declaration
Swift
public func singleValueContainer() -> SingleValueEncodingContainer
Return Value
A
SingleValueEncodingContainer
instance.
-
Stores a Date value for encoding.
Declaration
Swift
public func box(_ date: Date) throws -> Any
Parameters
value
The value to encode.
Return Value
A
boxed
version of the value that is safe for encoding. -
Stores a Data value for encoding.
Declaration
Swift
public func box(_ data: Data) throws -> Any
Parameters
value
The value to encode.
Return Value
A
boxed
version of the value that is safe for encoding. -
Stores any
Encodable
data type in a format suitable for encoding.Declaration
Swift
public func box<T : Encodable>(_ value: T) throws -> Any
Parameters
value
The value to encode.
Return Value
A
boxed
version of the value that is safe for encoding.