Coder Social home page Coder Social logo

asadeepdictionary's Introduction

More info: http://developing-ios-apps-with-andrew-shmig.blogspot.ru/2013/03/asadeepdictionary.html

ASADeepDictionary

Deep dictionary using KVC (+JSON)

Example #0:

JSONDataExample.json: http://paste.ubuntu.com/5609872/

  NSString *pathToJSON = [[NSBundle mainBundle] pathForResource:@"JSONDataExample"
                                                         ofType:@"json"];
  NSLog(@"pathToJSON: %@", pathToJSON);
  
  NSData *json = [[NSString stringWithContentsOfFile:pathToJSON
                                             encoding:NSUTF8StringEncoding
                                                error:nil] dataUsingEncoding:NSUTF8StringEncoding];
  
  ASADeepDictionary *dd = [[ASADeepDictionary alloc] initWithJSON:json];
  
  [dd setAlias:@"mainServlet" forKey:@"web-app.servlet"];
  [dd setAlias:@"logFile" forKey:@"init-param.dataLogLocation"];
  NSLog(@"dataLogLocation: %@", [dd valueForKey:@"$mainServlet.#4.$logFile"]);
  
  [dd release];
  dd = nil;

Example #1: ASADeepDictionary allocation and initialization (writing and reading values)

    ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
    
    [dd setValue:@"AndrewShmig" forKey:@"cookies.user.login"];
    [dd setValue:@"[email protected]" forKey:@"cookies.user.email"];
    [dd setValue:@"ru_RU" forKey:@"cookies.user.locale"];
    
    NSLog(@"login: %@", [dd valueForKey:@"cookies.user.login"]);
    NSLog(@"email: %@", [dd valueForKey:@"cookies.user.email"]);
    
    NSLog(@"user record: %@", [dd valueForKey:@"cookies.user"]);

Output:

    login: AndrewShmig
    email: [email protected] 
    user record: {
        email = "[email protected]";
        locale = "ru_RU";
        login = AndrewShmig;
    } 

Example #2: ASADeepDictionary to JSON convertion

    ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
    
    [dd setValue:@"AndrewShmig" forKey:@"cookies.user.login"];
    [dd setValue:@"[email protected]" forKey:@"cookies.user.email"];
    [dd setValue:@"ru_RU" forKey:@"cookies.user.locale"];
    
    NSData *jsonData = [dd JSON];
    NSLog(@"JSON: %@", [NSString stringWithUTF8String:[jsonData bytes]]);

Output:

    JSON: {
    "cookies" : {
        "user" : {
            "login" : "AndrewShmig",
            "email" : "[email protected]",
            "locale" : "ru_RU"
        }
    }
    }

Example #3: Init ASADeepDictionary with another dictionary

    NSDictionary *dic = @{@"window":@{
                              @"position": @{
                                  @"x": @"100",
                                  @"y": @"200",
                                  @"orientation":@{
                                      @"horizontalAlign": @"YES",
                                      @"verticalAlign": @"NO"
                                      }
                                  },
                              @"size": @{
                                  @"width": @"500",
                                  @"height": @"780"
                                  }
                              }};
    ASADeepDictionary *dd = [[ASADeepDictionary alloc] initWithDictionary:dic];
    
    NSLog(@"orientation: %@", [dd valueForKey:@"window.position.orientation"]);

Output:

    orientation: {
        horizontalAlign = YES;
        verticalAlign = NO;
    }

Example #4: Init ASADeepDictionary with JSON object

    NSDictionary *dic = @{@"key":@"value",
                          @"key2":@"value2",
                          @"user":@{
                              @"name":@"AndrewShmig",
                              @"email":@"[email protected]"
                              }};
    
    ASADeepDictionary *dd = [[ASADeepDictionary alloc]
                             initWithDictionary:dic];

    ASADeepDictionary *dd2 = [[ASADeepDictionary alloc]
                              initWithJSON:[dd JSON]];
    
    NSLog(@"%@", dd2);

Output:

    {
        key = value;
        key2 = value2;
        user =     {
            email = "[email protected]";
            name = AndrewShmig;
        };
    }

Example #5: Using aliases

    ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
    [dd setValue:@{@"login":@"AndrewShmig", @"email":@"[email protected]"}
          forKey:@"cookies.github.usergroup.admin"];
    
    [dd setAlias:@"adminInfo" forKey:@"cookies.github.usergroup.admin"];
    [dd setAlias:@"githubService" forKeyPath:@"cookies.github"];
    
    NSLog(@"login: %@", [dd valueForKey:@"$adminInfo.login"]);
    NSLog(@"email: %@", [dd valueForKey:@"$adminInfo.email"]);
    
    NSLog(@"githubService: %@", [dd valueForKey:@"$githubService"]);
    NSLog(@"github usergroup: %@", [dd valueForKey:@"$githubService.usergroup"]);
    
    [dd removeAlias:@"adminInfo"];
    
    NSLog(@"aliases: %@", [dd aliases]);
    
    NSLog(@"%@", dd);

Output:

    login: AndrewShmig
    email: [email protected]
    githubService: {
        usergroup =     {
            admin =         {
                email = "[email protected]";
                login = AndrewShmig;
            };
        };
    }
    
    github usergroup: {
        admin =     {
            email = "[email protected]";
            login = AndrewShmig;
        };
    }
    
    aliases: {
        "$githubService" = "cookies.github";
    }
    
    {
    cookies =     {
        github =         {
            usergroup =             {
                admin =                 {
                    email = "[email protected]";
                    login = AndrewShmig;
                };
            };
        };
    };
    }

Example #6: Array iteration with '#' placeholder

    ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
    
    NSArray *data = @[@{@"name":@"Andrew", @"age":@"21"},
                      @{@"name":@"Igori", @"age":@"25"},
                      @{@"name":@"Masha", @"age":@"19"}];
    
    [dd setAlias:@"data" forKey:@"cookies.localstoragetype.xml.data"];
    [dd setValue:data forKey:@"$data"];
    
    NSLog(@"%@", dd);
    
    NSUInteger count = [[dd valueForKey:@"$data"] count];
    for(int i=0; i<count; i++) {
      NSString *namePath = [NSString stringWithFormat:@"$data.#%d.name", i];
      NSString *agePath  = [NSString stringWithFormat:@"$data.#%d.age", i];
      
      NSString *name = [dd valueForKey:namePath];
      NSString *age = [dd valueForKey:agePath];
      
      NSLog(@"%@ is %@ years old!", name, age);
    }

Output:

    {
    cookies =     {
        localstoragetype =         {
            xml =             {
                data =                 (
                                        {
                        age = 21;
                        name = Andrew;
                    },
                                        {
                        age = 25;
                        name = Igori;
                    },
                                        {
                        age = 19;
                        name = Masha;
                    }
                );
            };
        };
    };
    }
    
    Andrew is 21 years old!
    Igori is 25 years old!
    Masha is 19 years old!

asadeepdictionary's People

Contributors

andrewshmig avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

uikit0

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.