Git Product home page Git Product logo

collectionutils's Introduction

CollectionUtils

CI Status Circle CI Coverage Status Carthage compatible Version License Platform

Useful utilities for Objective-C collection classes.

CollectionUtils/Compact

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.

Usage

Remove NSNull values from an array

NSArray *array = @[@"0", @"1", [NSNull null], @"2", [NSNull null], @"3"];
NSArray *compactArray = [array cu_compactArray];
//=> ["0", "1", "2", "3"]

Remove NSNull values from a dictionary

NSDictionary *dictionary = @{@"one": @"1",
                             @"null": [NSNull null],
                             @"two": @"2",
                             @"three": @"3"};
NSDictionary *compactDictionary = [dictionary cu_compactDictionary];
 //=> {"one": "1", "two": "2", "three": "3"}

Recursively remove all NSNull values by default

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"]

Remove all NSNull values from JSON returned from web services.

id object = [NSJSONSerialization JSONObjectWithData:data options:opt error:error];
id result =  [object cu_compactJSONObject];

CUJSONSerialization is a convienience class to remove all NSNull values from JSON data

id result = [CUJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

AFNetworking Additions

CUJSONResponseSerializer (for AFNetworking 2.x)

Install

pod "CollectionUtils-AFNetworking"

Usage

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);
}];

CUJSONRequestOperation (for AFNetworking 1.x)

Install

pod "CollectionUtils-AFNetworking-1.3"

Usage

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);
}];

Little performance penalty

How it works

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.

Requirements

  • iOS 4.3 or later
  • OS X 10.6 or later

Installation

CollectionUtils is available through CocoaPods, to install it simply add the following line to your Podfile:

pod "CollectionUtils"

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"

Author

kishikawa katsumi, [email protected]

License

CollectionUtils is available under the MIT license. See the LICENSE file for more info.

collectionutils's People

Contributors

kishikawakatsumi avatar norio-nomura avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.