Challenge solution(I think), and a question


Here’s what I ended up with:


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

    @autoreleasepool {
        //root dictionary
        NSMutableDictionary *rootObj = [[NSMutableDictionary alloc] init];
        NSMutableArray *stocks = [[NSMutableArray alloc] init];
        NSMutableDictionary *stock;

        // array (of dictionarys)
        stock = [NSMutableDictionary dictionary];
        [stock setObject:@"AAPL" forKey:@"symbol"];
        [stock setObject:[NSNumber numberWithInt:200] forKey:@"shares"];
        [stocks addObject:stock];
        stock = [NSMutableDictionary dictionary];
        [stock setObject:@"GOOG" forKey:@"symbol"];
        [stock setObject:[NSNumber numberWithInt:160] forKey:@"shares"];
        [stocks addObject:stock];
        [rootObj setObject:stocks forKey:@"stocks"];
        NSMutableDictionary *bio = [NSMutableDictionary dictionary];
        [bio setObject:@"Sir John A. MacDonald" forKey:@"name"];
        [bio setObject:@"Prime Minister" forKey:@"title"];
        [rootObj setObject:bio forKey:@"founder"];
        // string
        NSString *stockString = @"Canada";
        [rootObj setObject:stockString forKey:@"Location"];
        // data - reads google.png file from earlier chapter
        NSData *readData = [NSData dataWithContentsOfFile:@"/tmp/google.png"];
        [rootObj setObject:readData forKey:@"image"];
        // date
        NSDate *dob = [NSDate dateWithString:@"1867-07-01 12:00:00 +0600"];
        [rootObj setObject:dob forKey:@"ConfederationDate"];
        NSNumber *intVar = [NSNumber numberWithInt:1];
        NSNumber *floatVar = [NSNumber numberWithFloat:1.0];
        NSNumber *boolVar = [NSNumber numberWithBool:YES];
        [rootObj setObject:intVar forKey:@"int"];
        [rootObj setObject:floatVar forKey:@"float"];
        [rootObj setObject:boolVar forKey:@"boolean"];                

        // write out to the plist        
        [rootObj writeToFile:@"/tmp/challenge.plist" atomically:YES];
        //read the plist
        NSDictionary *challengeList = [NSDictionary dictionaryWithContentsOfFile:@"/tmp/challenge.plist"];
        NSLog(@"challengeList %@", challengeList);
    return 0;

My question is, since we want a plist with multiple types (array, dictionary, string, data, date, integer, float, boolean), does the root object for the property list has to be a dictionary?


No, the root object can be any of the property list types, but it is typically a dictionary or an array.