Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

First Objective-C Code

I'm a pretty experienced coder. I'm moving into OC which is obviously a departure from most syntax that I am accustom to. So I'm working through the abstraction process that I think I have in my head. Considering this is day one, considering I've opted to start coding some of my own things in tandem with the coursework, and considering I don't want to walk too far down the road of bad habits, I'd love some opinions on what this first bit of code looks like, what I'm doing wrong, what I can do to make improvements in syntax and readability.

Synopsis: This is loading a local json file to put together basic http request options which in turn will create a http request to a simplistic rest server which will return some variables and move on.

This is all very new to me, so I'd appreciate experienced eyes.

TIA!

// Get the local configuration file
    NSString *config = [[NSBundle mainBundle]
                        pathForResource:@"config"
                        ofType:@"json"
                        inDirectory:@"config"];

    // This so that we can access the error if something goes wrong
    NSError *error = nil;

    // Get the json data
    NSMutableData *json = [NSData
                    dataWithContentsOfFile:config
                    options:NSDataReadingMappedIfSafe
                    error:&error];

    // Get the json data
    NSMutableDictionary *variables = [NSJSONSerialization
                               JSONObjectWithData:json
                               options:NSJSONReadingAllowFragments
                               error:&error];

    // some helpful debugging
    NSLog(@"Loaded config %@.", variables );

    // Let's contstruct a url for fetching the remainder of the configurations
    NSInteger ssl = [variables
                     objectForKey:@"ssl"];

    // some helpful debugging
    NSLog(@"Setting ssl enabled %@.", ssl );

    // Let's contstruct a url for fetching the remainder of the configurations
    NSString *scms_mode = [[variables
                           objectForKey:@"querystring"]
                           objectForKey:@"mode"];

    // some helpful debugging
    NSLog(@"Setting mode querystring %@.", scms_mode );

    // Let's contstruct a url for fetching the remainder of the configurations
    NSString *scms_key = [[variables
                           objectForKey:@"querystring"]
                           objectForKey:@"key"];

    // some helpful debugging
    NSLog(@"Setting key querystring %@.", scms_key );

    // Let's contstruct a url for fetching the remainder of the configurations
    NSString *key = [variables
                             objectForKey:@"key"];

    // some helpful debugging
    NSLog(@"Setting key %@.", key );

    // Get a list of servers
    NSDictionary *servers = [variables
                             objectForKey:@"servers"];

    // some helpful debugging
    NSLog(@"Listing servers %@.", servers );

    // Let's contstruct a url for fetching the remainder of the configurations
    NSString *server = [variables
                        objectForKey:@"server"];

    // some helpful debugging
    NSLog(@"Setting server %@.", server );

    // Let's contstruct a url for fetching the remainder of the configurations
    NSURL *url = [NSURL
                  URLWithString:[NSString
                                 stringWithFormat:@"http%@://%@/?%@&%@=%@",
                                 ((ssl==1)?@"s":@""),
                                 [servers objectForKey:server],
                                 scms_mode,
                                 scms_key,
                                 key]
                  ];

    // some helpful debugging
    NSLog(@"Setting created url %@.", url );

    // Get the json data
    json = [NSData
            dataWithContentsOfURL:url
            options:NSDataReadingUncached
            error:&error];

    // some helpful debugging
    NSLog(@"Loaded config from url %@.", json );

    // Turn the
    variables = [NSJSONSerialization
                 JSONObjectWithData:json
                 options:NSJSONReadingAllowFragments
                 error:&error];

    // some helpful debugging
    NSLog(@"Returned remove variables %@.", variables ); ```