Action Codable
For iOS, tvOS and macOS development with Swift and XcodeAvailable exclusively as part of the Action Data framework.
Codable
, Encodable
and Decodable
protocols to move information between your data models and our portable ADRecord
and ADRecordSet
formats.
With Action Codable and Action Data Providers, build your data model objects as simple struct
or class
objects and inherit from ADDataTable
, then use Action Controlsto quickly create, insert, update, delete and maintain the tables and records in the underlying data source. For example:
import Foundation
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() {
}
}
This includes support for complex tables with nested objects, arrays and dictionaries and complex relationships such as one-to-one, one-to-many and many-to-many. For example:
import Foundation
import ActionUtilities
import ActionData
struct Address: Codable {
var addr1 = ""
var addr2 = ""
var city = ""
var state = ""
var zip = ""
}
class Person: ADDataTable {
static var tableName = "People"
static var primaryKey = "id"
static var primaryKeyType = ADDataTableKeyType.autoUUIDString
var id = UUID().uuidString
var firstName = ""
var lastName = ""
var addresses: [String:Address] = [:]
required init() {
}
init(firstName: String, lastName:String, addresses: [String:Address] = [:]) {
self.firstName = firstName
self.lastName = lastName
self.addresses = addresses
}
}
class Group: ADDataTable {
static var tableName = "Groups"
static var primaryKey = "id"
static var primaryKeyType = ADDataTableKeyType.autoUUIDString
var id = UUID().uuidString
var name = ""
var people = ADCrossReference<Person>(name: "PeopleInGroup", leftKeyName: "groupID", rightKeyName: "personID")
required init() {
}
init(name: String, people: [Person] = []) {
self.name = name
self.people.storage = people
}
}
Action Codable includes the following elements:
- ADKey
- ADRecord
- ADRecordSet
- ADInstanceArray
- ADInstanceDictionary
- ADEncodingStorage
- ADDecodingStorage
- SQLite
- SPON
Shared Elements
These are common elements shared across all custom coders in the Action Codable suite.
ADKey
Defines a Coding Key for use when providing a custom Coder for Action Data types. This includes the standard key used for a “super encoder”, a string value used for path type keys (KeyedEncodingContainers) and an integer value used for index type keys (UnkeyedEncodingContainers).
ADRecord
Defines a ADRecord
as a dictionary of Key/Value pairs where the Key is a String
and the Value is Any
type. A ADRecord
can be returned from or sent to a ADDataProvider
or any of the Action Codable controls.
Example:
let provider = ADSQLiteProvider.shared
let record = try provider.query("SELECT * FROM Categories WHERE id = ?", withParameters: [1])
print(record["name"])
ADRecordSet
Defines an array of ADRecord
instances that can be sent to or returned from a ADDataProvider
or any of the Action Codable controls.
Example:
let provider = ADSQLiteProvider.shared
let records = try provider.getRows(Category.self)
for record in records {
print(record["name"])
}
ADInstanceArray
Defines a passable array of values used as temporary storage when encoding or decoding an Action Data class. ADInstanceArray
also introduces support for the new Swift Portable Object Notation (SPON) data format that allows complex data models to be encoded in a portable text string that encodes not only property keys and data, but also includes type information about the encoded data. For example:
@array[1!,2!,3!,4!]
The portable, human-readable string format encodes values with a single character type designator as follows:
%
– Bool!
– Int$
– String^
– Float&
– Double*
– EmbeddedNSData
orData
valueAdditionally, embedded arrays will be in the@array[...]
format and embedded dictionaries in the@obj:type<...>
format.
ADInstanceDictionary
Defines a passable dictionary of ADRecord
values when encoding or decoding an Action Data class instance. ADInstanceDictionary
also introduces support for the new Swift Portable Object Notation (SPON) data format that allows complex data models to be encoded in a portable text string that encodes not only property keys and data, but also includes type information about the encoded data. For example:
@obj:Address<state$=`TX` city$=`Seabrook` addr1$=`25 Nasa Rd 1` zip$=`77586` addr2$=`Apt #123`>
The portable, human-readable string format encodes values with a single character type designator as follows:
%
– Bool!
– Int$
– String^
– Float&
– Double*
– EmbeddedNSData
orData
valueAdditionally, embedded arrays will be in the@array[...]
format and embedded dictionaries in the@obj:type<...>
format.
ADEncodingStorage
Holds information about a given Action Data class while it is being encoded.
ADDecodingStorage
Holds information about a given Action Data object while it is being decoded.
SQLite
Contains the custom encoder and decoder to preprocess and post process data object models that will be sent to or returned from a ADSQLiteProvider
and store or returned from a backing SQLite database.
A ADSQLEncoder
can return a ADRecord
, ADInstanceArray
or ADInstanceDictionary
representing a given data object model or collection of object models.
A ADSQLDecoder
takes a ADRecord
, ADInstanceArray
or ADInstanceDictionary
returned from a data source and converts it into a data object model or a collection of object models representing the given information.
SPON
Contains the custom encoder and decoder to preprocess and post process data object models that will be sent to or returned from a ADSPONProvider
and store or returned from a backing Swift Portable Object Notation (SPON) database.
A ADSPONEncoder
can return a ADRecord
, ADInstanceArray
or ADInstanceDictionary
representing a given data object model or collection of object models.
A ADSPONDecoder
takes a ADRecord
, ADInstanceArray
or ADInstanceDictionary
returned from a data source and converts it into a data object model or a collection of object models representing the given information.