Skip to content

Instantly share code, notes, and snippets.

@CreatureSurvive
Last active November 7, 2024 18:45
Show Gist options
  • Select an option

  • Save CreatureSurvive/75faeb943b8951ced44ed28cde227e95 to your computer and use it in GitHub Desktop.

Select an option

Save CreatureSurvive/75faeb943b8951ced44ed28cde227e95 to your computer and use it in GitHub Desktop.

Revisions

  1. CreatureSurvive revised this gist Nov 8, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ObjectForKeypath.m
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    /*
    * usage:
    * [self objectForKeypath:@"someKey.3.someNestedKey.2" inJSON:myJSONObject];
    */
    - (id)objectForKeypath:(NSString *)keypath inJSON:(NSData *)json {
    if (!keypath || !json) {return nil;}

  2. CreatureSurvive revised this gist Nov 8, 2018. 1 changed file with 3 additions and 8 deletions.
    11 changes: 3 additions & 8 deletions ObjectForKeypath.m
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,7 @@ - (id)objectForKeypath:(NSString *)keypath inJSON:(NSData *)json {

    __block NSInteger depth = 0;
    NSArray *keys = [keypath componentsSeparatedByString:@"."];
    id result = json;

    id (^collection)(id) = ^id(id data) {
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    };
    id result = [NSJSONSerialization JSONObjectWithData:json options:kNilOptions error:nil];

    id (^objectAtPath)(NSString *, id) = ^id(NSString *path, id collection) {
    if (collection) {
    @@ -26,11 +22,10 @@ - (id)objectForKeypath:(NSString *)keypath inJSON:(NSData *)json {
    };

    while (depth < keys.count) {
    id data = collection(result);

    if (!data) { return nil; }
    if (!result) { return nil; }

    result = objectAtPath(keys[depth], data);
    result = objectAtPath(keys[depth], result);
    }

    return result;
  3. CreatureSurvive revised this gist Nov 8, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ObjectForKeypath.m
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,11 @@ - (id)objectForKeypath:(NSString *)keypath inJSON:(NSData *)json {

    depth++;
    if ([collection isKindOfClass:[NSDictionary class]]) {
    return collection[path];
    return [(NSDictionary *)collection objectForKey:path];
    }

    else if ([collection isKindOfClass:[NSArray class]]) {
    return collection[[path integerValue]];
    return [(NSArray *)collection objectAtIndex:[path integerValue]];
    }
    }

    @@ -34,4 +34,4 @@ - (id)objectForKeypath:(NSString *)keypath inJSON:(NSData *)json {
    }

    return result;
    }
    }
  4. CreatureSurvive created this gist Nov 8, 2018.
    37 changes: 37 additions & 0 deletions ObjectForKeypath.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    - (id)objectForKeypath:(NSString *)keypath inJSON:(NSData *)json {
    if (!keypath || !json) {return nil;}

    __block NSInteger depth = 0;
    NSArray *keys = [keypath componentsSeparatedByString:@"."];
    id result = json;

    id (^collection)(id) = ^id(id data) {
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    };

    id (^objectAtPath)(NSString *, id) = ^id(NSString *path, id collection) {
    if (collection) {

    depth++;
    if ([collection isKindOfClass:[NSDictionary class]]) {
    return collection[path];
    }

    else if ([collection isKindOfClass:[NSArray class]]) {
    return collection[[path integerValue]];
    }
    }

    return nil;
    };

    while (depth < keys.count) {
    id data = collection(result);

    if (!data) { return nil; }

    result = objectAtPath(keys[depth], data);
    }

    return result;
    }