Obsidian iOS SDK 为 Obsidian 服务器 API 提供了 Swift 接口。按照以下说明进行操作,以便快速开始。
Obsidian iOS SDK 允许轻松管理环境。枚举 Environment
包含三种状态:Development
、Staging
和 Production
。出于演示目的,我们将配置 Development
状态以连接到本地运行的服务器。
let baseURL = NSURL(string: "http://127.0.0.1:8000")!
Environment.Development.configure(
key: "<your client key>",
secret: "<your client secret>",
base: baseURL
)
接下来,我们将激活 Development
环境
Environment.Development.setCurrent()
此初始配置通常在应用程序的代理的 方法中执行。
有关更多信息,请参阅文档中的 Environment
部分。
您应用程序的所有资源应从 Resource
类继承。通过这样做,您的资源可以利用 SDK 的值映射系统,以及一些有用的便捷方法。
为了进行演示,我们假设我们有一个称为 employee
的资源在服务器的 resources.json
文件中定义。我们还将假设员工资源在其定义中指定了一个类型为 string
的属性 address
。
当我们对 Resource
进行子类化时,我们必须覆盖 var name: String
以及 required init(mapper: Mapper)
。
import Foundation
import Obsidian_iOS_SDK
class Employee: Resource {
// This class variable provides the SDK with the class's corresponding server-side resource name
override class var name: String {
return "employee"
}
// This property's setter is private so that it can be manipulated by the Mapper
private(set) var address: String!
// This initializer is called every time an Employee needs to be deserialized
required init(mapper: Mapper) {
/*
** The <- operator defines a mapping between an attribute's server side
** representation (mapper["address"]) and its local property (self.address).
*/
address <- mapper["address"]
super.init(mapper: mapper)
}
}
有关更多信息,请参阅文档中的 Resource
和 Mapper
部分。
为了查阅一些员工信息,我们将使用 Resource
类提供的 read
类方法
Employee.read { (response: Response<Employee>) -> Void in
// Since Response is an enum, we can switch over the possible outcomes
switch(response) {
// If the operation succeeded, print the address of the first result
case .Success(let results):
println(results.firstResult?.address)
break
/*
** If the operation failed, log it to the console.
** NOTE: There are more descriptive error states defined in the Response Enum Reference.
*/
default:
print("oh no! an error occurred!")
break
}
}
内置的 create
、update
和 delete
请求的工作方式类似。有关更多信息,请参阅文档。
在 CocoaDocs 上查看 文档
请将所有错误、功能请求和其他问题提交给Nick Lee,[email javascript:;protected]
Obsidian iOS SDK在MIT许可协议下发布。请参阅LICENSE文件获取详细信息。