Latest 1.3.0
Homepage https://github.com/shenhai193/SHValue
License MIT
Platforms osx 10.9, ios 8.0, watchos 2.0, tvos 9.0, requires ARC
Authors

Travis CI Carthage compatible CocoaPods Platform

The safe way to deal with JSON data in Objective-C

Why is the typical JSON handling in Objective-C necessary?

The code would look like this:

NSDictionary *jsonDict = @{@"data": @[@{}, @{}, @{@"name": @"A"}]};
if ([jsonDict isKindOfClass:[NSDictionary class]]) {
    if ([jsonDict[@"data"] isKindOfClass:[NSArray class]]) {
        NSArray *array = jsonDict[@"data"];
        if (array.count > 2) {
            NSString *name = array[2];
            // Finally we got the name
        }
    }
}

It’s not good.

With SHValue all you have to do is:

NSDictionary *jsonDict = @{@"data": @[@{}, @{}, @{@"name": @"A"}]};
NSString *name = SHVALUE(jsonDict)[@"data"][2].string;
// There's our name

// or 
NSString *name = [SHValue value:jsonDict][@"data"][2].string;

NSString *str = SHVALUE(jsonDict)[@"a"][@"b"][@"c"][100].string;
// The str is nil, but it won't crash. 

NSString *str = SHVALUE(jsonDict)[@"a"][@"b"][@"c"][100].stringValue;  // str is ""
// If you want it to be a string and not nil, you should use stringValue.

Don’t worry about the access to cross-border issues. It’s done for you automatically and safely.

It supports generics at the same time. The T is custom class.

T *t = [SHValue<T> value:obj][@"a"][@"b"][100].value;

The string perproty is nullable (Optional), stringValue is Non-optional string.
Even if there is no value, the stringValue will return "".
It also supports array, dictionary and number, optional and non-optional.

Requirements

  • iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+
  • Xcode 8

Integration

CocoaPods (iOS 8+, OS X 10.9+)

You can use CocoaPods to install SHValue by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
pod 'SHValue'
end

Note that this requires CocoaPods version 36, and your iOS deployment target to be at least 8.0:

Latest podspec

{
    "name": "SHValue",
    "version": "1.3.0",
    "summary": "The safe way to deal with JSON data in Objective-C.",
    "homepage": "https://github.com/shenhai193/SHValue",
    "license": {
        "type": "MIT",
        "file": "FILE_LICENSE"
    },
    "authors": {
        "Charles Zou": "[email protected]"
    },
    "requires_arc": true,
    "platforms": {
        "osx": "10.9",
        "ios": "8.0",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/shenhai193/SHValue.git",
        "tag": "1.3.0"
    },
    "source_files": "Source/*.{h,m}"
}

Pin It on Pinterest

Share This