TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by kishikawa katsumi.
Depends on: | |
CollectionUtils | >= 0 |
AFNetworking | ~> 1.3.3 |
Useful utilities for Objective-C collection classes.
Subclasses of NSArray and NSDictionary to recursively remove all NSNull values automatically with little performance penalty.
It is useful for JSON returned from web services.
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
and CUCompactDictionary
just wrap the original array/dictionary to behave as if do not have NSNull
values.
To remove NSNull
values for the nested collection is delayed until the nested array/dictionary is really accessed.
It is performed only for one level at a time.
If you want to use AFNetworking additions, add the following line:
pod "CollectionUtils-AFNetworking"
if you use older version AFNetworking,
pod "CollectionUtils-AFNetworking-1.3"
kishikawa katsumi, [email protected]
CollectionUtils is available under the MIT license. See the LICENSE file for more info.