Skip to content

nicolasnascimento/SwiftAirtable

Repository files navigation

Update

For a more modern implementation, see AirtableKit

SwiftAirtable

Alt Text

Description

An unofficial Swift interface to Airtable's REST API

Usage

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

Create

airtable.createObject(with: object, inTable: table) { object: AirtablePerson, error: Error? in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Retrieve

airtable.fetchAll(table: table) { (objects: [AirtablePerson], error: Error?) in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Update

airtable.updateObject(with: object, inTable: table) { object: AirtablePerson?, error: Error? in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Delete

airtable.deleteObject(with: object, inTable: table) { sucess, error in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Example

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.

Installation

SwiftAirtable is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftAirtable'

Author

Nicolas Nascimento, [email protected]

Sourcery

An stencil file is provided in the repo that allows you generate code for the AirtableObject protocol using Sourcery (https://github.com/krzysztofzablocki/Sourcery)

License

SwiftAirtable is available under the MIT license. See the LICENSE file for more info.