For a more modern implementation, see AirtableKit
An unofficial Swift interface to Airtable's REST API
In order to make use of the Framework, simply create a strucuture
struct AirtablePerson {
// The airtable object id
var id: String = ""
var name: String = ""
}
and make it adopt the AirtableObject protocol
extension AirtablePerson: AirtableObject {
static var fieldKeys: [(fieldName: String, fieldType: AirtableTableSchemaFieldKey.KeyType)] {
var fields = [(fieldName: String, fieldType: AirtableTableSchemaFieldKey.KeyType)]()
fields.append((fieldName: AirtableField.name.rawValue, fieldType: .singleLineText))
return fields
}
func value(forKey key: AirtableTableSchemaFieldKey) -> AirtableValue? {
switch key {
case AirtableTableSchemaFieldKey(fieldName: AirtableField.name.rawValue, fieldType: .singleLineText): return self.name
default: return nil
}
}
init(withId id: String, populatedTableSchemaKeys tableSchemaKeys: [AirtableTableSchemaFieldKey : AirtableValue]) {
self.id = id
tableSchemaKeys.forEach { element in
switch element.key {
case AirtableTableSchemaFieldKey(fieldName: AirtableField.name.rawValue, fieldType: .singleLineText): self.name = element.value.stringValue
default: break
}
}
}
}
Finally create an Airtable to perform the operations
let airtable = Airtable(apiKey: apiKey, apiBaseUrl: apiBaseUrl, schema: AirtablePerson.schema)
You can perform all standard CRUD operations
airtable.createObject(with: object, inTable: table) { object: AirtablePerson, error: Error? in
if let error = error {
// Error Code
} else {
// Success Code
}
}
airtable.fetchAll(table: table) { (objects: [AirtablePerson], error: Error?) in
if let error = error {
// Error Code
} else {
// Success Code
}
}
airtable.updateObject(with: object, inTable: table) { object: AirtablePerson?, error: Error? in
if let error = error {
// Error Code
} else {
// Success Code
}
}
airtable.deleteObject(with: object, inTable: table) { sucess, error in
if let error = error {
// Error Code
} else {
// Success Code
}
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
You'll need to provide your API Key and Base URL.
SwiftAirtable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftAirtable'
Nicolas Nascimento, [email protected]
An stencil file is provided in the repo that allows you generate code for the AirtableObject protocol using Sourcery (https://github.com/krzysztofzablocki/Sourcery)
SwiftAirtable is available under the MIT license. See the LICENSE file for more info.