My Solution for Property List


#1

#import <Foundation/Foundation.h>

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

    @autoreleasepool {

        NSURL *url = [NSURL URLWithString:@"http:www.google.com/images/logos/ps_logo2.png"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSError *error = nil;
        
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                            returningResponse:nil
                                                        error:&error];
        
                                    if (data == nil) {
                                        NSLog(@"download failed: %@", [error localizedDescription]);
                                        return 1;
                                    }
                                    
        NSNumber *written = [[NSNumber alloc]initWithBool:[data writeToFile:@"/tmp/googletest.png"
                                                                    options:NSDataWritingAtomic
                                                                      error:&error]];
        if ([written boolValue] == NO) {
            NSLog(@"The write failed: %@", [error localizedFailureReason]);
            return 1;
        }
        
        
        // The main array
        NSMutableArray *pList = [[NSMutableArray alloc]init];
        
        NSArray *colors = @[@"Blue", @"Red", @"Green", @"Black", @"Pink", @"Purple"];
        
        // Make a pretty formatted date
        NSDateFormatter *dateFormatter= [[NSDateFormatter alloc]init];
        [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
        
        NSDate *now = [NSDate date];
        
        
        NSDictionary *stock = [[NSDictionary alloc]
                               initWithObjects:
                                               @[@"GSAT", [NSNumber numberWithInt:200], [NSNumber numberWithFloat:2000.34],now]
                                       forKeys:@[@"Symbol", @"Shares", @"PurchasePrice", @"PurchaseDate"]];
        
        // Adding objects below to main mutable array.
        
        [pList addObject:stock]; // Add dictionary with int, float, and date
        [pList addObject:colors]; // Add an Array
        [pList addObject:[dateFormatter stringFromDate:now]];  // correct date and time but adds a NSString
        [pList addObject:now]; // Shows date in UTC and adds a NSDate
        [pList addObject:written]; // Add a bool
        [pList addObject:data]; // Add a NSData
        
        
        [pList writeToFile:@"/tmp/myPlist" atomically:YES];
        
    }
    return 0;
}