测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | BSD |
发布最新发布 | 2016年9月 |
SPM支持 SPM | ✓ |
依赖关系 | |
RestGoatee-Core | ~> 2.5.5 |
AFNetworking | ~> 3.0 |
RestGoatee 是一个 AFNetworking 的扩展框架;它将原始的 NSDictionary
和 NSXMLParser
响应便捷地转换为您的对象。如果您有 AFNetworking >= 3.0,此库将使用该版本; 否则,它将作为 CocoaPods pod install
过程的一部分被包含。
此库仅处理网络,并使用底层库 RestGoatee-Core 对 JSON 和 XML 进行反序列化。
支持:iOS 7.0+ 和 AFNetworking 3.x,低于 2.6 的版本支持 AFNetworking 2.x,v1.5.4 分支支持到 AFNetworking 1.3.3(没有 XML 支持)。
此库的目标是在常见情况下保持简单,在一般情况下保持可扩展性
1) "API 响应用到工作对象" 操作不是进行业务逻辑或键转换的地方。
2) API 层应该能够无缝地处理新对象和对象属性,而无需新的反序列化逻辑。例如,此 提交 在未引起任何异议的情况下添加了一个完全新的响应对象到示例项目中。
3) 由于 JSON 和 XML 类型有限,反序列化器需要能够智能地映射到更广泛的标准类型集合。
4) 通常不在项目初期实现 CoreData 支持;此库使启用它更加容易,且无需太多的重构。CoreData 支持是隐含的,但在没有它的项目中不活跃。
5) 默认映射行为应该既直观(大部分时间正确)又可扩展。
6) 默认应该以最少的复杂性和代码行数进行。对于明显一对一且命名良好的对象,您不需要指定映射。
考虑您最喜欢的或最受欢迎的模型框架
使用 cocoapods 在您的 Podfile 中添加 pod 'RestGoatee'
并运行 pod install
。没有 cocoapods 的人可以将顶层文件夹 "RestGoatee" 包含在他们的存储库中。使用 #import <RestGoatee/RestGoatee.h>
包含所有公共头文件并开始使用库。
如果您实现了 -keyForReconciliationOfType:
,则键应该是所有对象上都可用的一般性东西(例如唯一的标识符键)。没有这个键的对象将不会进行唯一检查,此外它不影响非 NSManagedObject
子类。
您可以克隆此存储库并在安装目录中运行 pod install
。
我们将使用此示例将 iTunes 搜索 API 的请求转换为对象。
@interface RGBook : NSObject
@property (nonatomic, strong) NSString* artistName;
@property (nonatomic, strong) NSString* description;
@property (nonatomic, strong) NSArray* genres;
@property (nonatomic, strong) NSDate* releaseDate;
@property (nonatomic, strong) NSNumber* trackId;
@property (nonatomic, strong) NSString* trackName;
@end
*** 关于使用的说明:完全尊重属性的语义。一个属性可以被声明为 (copy, readonly)
,并且会被正确处理。通过其支持实例变量构建 readonly
属性,通过发送 -copy
等发送 copy
。
@implementation RGBook @end //nothing!
- (void) getResults:(NSString*)searchTerm completion:(RGResponseBlock)completion {
/* your invocation of the API */
[self GET:@"/search" parameters:@{ @"term" : searchTerm } keyPath:@"results" class:[RGBook class] completion:completion];
}
另一个选项是检索原始对象
- (void) getResults:(NSString*)searchTerm completion:(RGResponseBlock)completion {
[self GET:@"/search" parameters:@{ @"term" : searchTerm } keyPath:@"results" class:Nil completion:^(RGResponseObject* response) {
NSLog(@"%@", response.responseBody);
}];
}
此 API 客户端可以处理 XML,这将返回原始的 RGXMLNode
文档节点
- (void) getBartStations {
[self GET:@"http://api.bart.gov/api/stn.aspx" parameters:nil keyPath:@"root.stations.station" class:Nil completion:^(RGResponseObject* response) {
NSLog(@"%@", response.responseBody);
}];
}
此调用将返回一个 RDDBartStation
数组
- (void) getBartStations {
[self GET:@"http://api.bart.gov/api/stn.aspx" parameters:nil keyPath:@"root.stations.station" class:[RDDBartStation self] completion:^(RGResponseObject* response) {
NSLog(@"%@", response.responseBody);
}];
}
图像下载已优化并缓存,用法可以是针对 UIImageView
或者直接这样调用
UIImageView* imageView = [UIImageView new];
[imageView rg_setImageWithURL:@"http://placekitten.com/200/200"];
- 或者 -
NSURL* url = [NSURL URLWithString:@"http://placekitten.com/200/200"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
rg_setImageWithURL(nil, request, nil, ^(NSHTTPURLResponse* response, UIImage* image) {
NSLog(@"%@", image);
/* Do something with your image */
}, nil);
简化版 BSD (2条款)