SwiftAirtable
描述
Airtable REST API 的非官方 Swift 接口
用法
为了使用框架,只需创建一个结构体
struct AirtablePerson {
// The airtable object id
var id: String = ""
var name: String = ""
}
并使其遵循 AirtableObject 协议
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
}
}
}
}
最后创建一个 Airtable 执行操作
let airtable = Airtable(apiKey: apiKey, apiBaseUrl: apiBaseUrl, schema: AirtablePerson.schema)
您可以执行所有标准的 CRUD 操作
创建
airtable.createObject(with: object, inTable: table) { object: AirtablePerson, error: Error? in
if let error = error {
// Error Code
} else {
// Sucess Code
}
}
检索
airtable.fetchAll(table: table) { (objects: [AirtablePerson], error: Error?) in
if let error = error {
// Sucess Code
} else {
// Erro Code
}
}
更新
airtable.updateObject(with: object, inTable: table) { object: AirtablePerson?, error: Error? in
if let error = error {
// Sucess Code
} else {
// Erro Code
}
}
删除
airtable.deleteObject(with: object, inTable: table) { sucess, error in
if let error = error {
// Error Code
} else {
// Sucess Code
}
}
示例
要运行示例项目,请先克隆仓库,然后从示例目录运行pod install
。您需要提供您的API密钥和基本URL。
安装
SwiftAirtable可以通过CocoaPods使用。要安装它,只需在你的Podfile中添加以下一行
pod 'SwiftAirtable'
作者
Nicolas Nascimento, [email protected]
Soucery
仓库中提供了一个模板文件,可以用来生成遵循AirtableObject协议的代码(使用Sourcery)。https://github.com/krzysztofzablocki/Sourcery
许可证
SwiftAirtable 在MIT许可证下可用。更多信息请查看LICENSE文件。