测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可协议 | MIT |
Published最后发布 | 2015年9月 |
依赖 | |
CollectionUtils | >= 0 |
AFNetworking | ~> 2.0 |
Objective-C集合类的实用工具。
NSArray 和 NSDictionary 的子类,自动递归删除所有 NSNull 值,性能损耗很小。
这对于来自 Web 服务的 JSON 非常有用。
NSArray *array = @[@"0", @"1", [NSNull null], @"2", [NSNull null], @"3"];
NSArray *compactArray = [array cu_compactArray];
//=> ["0", "1", "2", "3"]
NSDictionary *dictionary = @{@"one": @"1",
@"null": [NSNull null],
@"two": @"2",
@"three": @"3"};
NSDictionary *compactDictionary = [dictionary cu_compactDictionary];
//=> {"one": "1", "two": "2", "three": "3"}
NSArray *array = @[@"0",
@"1",
[NSNull null],
@"2",
@{@"one": @"1",
@"null": [NSNull null],
@"two": @"2",
@"three": @"3"},
@"4"];
NSMutableArray *compactArray = [array cu_compactArray];
//=> ["0", "1", "2", {"one": "1", "two": "2", "three": "3"}, "4"]
id object = [NSJSONSerialization JSONObjectWithData:data options:opt error:error];
id result = [object cu_compactJSONObject];
id result = [CUJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
pod "CollectionUtils-AFNetworking"
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [CUJSONResponseSerializer serializer];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
pod "CollectionUtils-AFNetworking-1.3"
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/resources.json"]];
CUJSONRequestOperation *operation = [CUJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"JSON: %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Error: %@", error);
}];
[operation start];
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://example.com/"]];
[client registerHTTPOperationClass:[CUJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
[client getPath:@"resouce.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
CUCompactArray
和 CUCompactDictionary
只是对原始数组/字典进行封装,以便像没有 NSNull
值一样操作。
移除嵌套集合中的 NSNull
值会延迟到真正访问嵌套数组/字典时执行。
它一次只对一层执行。
如果您想使用 AFNetworking 扩展,请添加以下行
pod "CollectionUtils-AFNetworking"
如果您使用的是旧版本的 AFNetworking,
pod "CollectionUtils-AFNetworking-1.3"
kishikawa katsumi, [email protected]
CollectionUtils 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。