测试测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2017年6月 |
SwiftSwift版本 | 3.0 |
SPM支持SPM | ✗ |
由Daniel Green维护。
依赖关系 | |
Alamofire | ~> 4.0.1 |
Placemat | >= 0.6.0 |
午餐旨在实现尽可能简单和精简的配置。开发一个与REST API通信的应用程序应该非常简单。其他试图解决相同问题的项目最终总是会有很多样板代码或映射关系。
目前,兼容传统由Rails提供的REST API。也许(希望如此)在未来,午餐将覆盖绝大多数REST服务。欢迎发送任何拉取请求!
以下是午餐的一些功能:
本地/远程模型之间完整资源映射
Post
模型与路由/posts
匹配。id
属性会自动映射到本地model.remoteId
(或者如果您想覆盖默认行为,可以是任何其他本地变量)。model.remote.id
是否存在,调用save()
将创建或更新资源(即POST /posts
或PATCH /posts/42
)。model.remote.save
时,只有自获取以来已更改的属性才会发送到服务器。snake_case
属性映射到Swift标准camelCase
,反之亦然。错误处理
UIAlertView
中提醒。为了避免最终用户被过度淹没,只会提醒第一个错误。Luncheon.Options.errorHandler
)。可以通过attributes()
和attributesUnderscore()
轻松获取模型属性值作为字典。
灵活性
源代码中包含了一个示例应用程序。
配置可以像提供baseUrl一样简单
Luncheon.Options.baseUrl = "http://jsonplaceholder.typicode.com"
定义一个模型
class Post: NSObject, Lunch {
dynamic var title : String?
required override init() {
super.init()
}
}
获取并打印出帖子列表
// GET /posts
Post.remote.all { (posts: [Post] in
for post in posts {
println(post.title)
}
}
创建一个新的帖子并打印出响应资源的属性
let myPost = Post()
myPost.title = "Wow, Luncheon saves me so much time"
// POST /posts
myPost.remote.save { post in
println(post.attributes())
}
获取现有的帖子并打印其属性
// GET /posts/42
Post.remote.find(42) { post in println(post.attributes()) }
或者一个帖子的一些关联评论
// GET /posts/42/comments
post.remote.associated(Comment.self).all { (comments: [Comment]) in
for comment in comments {
println(comment.title)
}
}
Luncheon可以通过CocoaPods使用。要安装它,只需将以下行添加到您的Podfile中
pod "Luncheon", git: "https://github.com/Dan2552/Luncheon.git"
Luncheon受MIT许可证的许可。有关更多信息,请参阅LICENSE文件。