Property list challenge : question



For this challenge, I wanted to create a property list for computers with their specs and image.
As I wanted to test if I could re-use the image stored in my p-list, I wondered if there was a way to create a new image each time, in this part of the code :

[color=#008040] for (NSDictionary *d in localPropertyList) {
NSLog(@“I have a %@”,[d objectForKey:@“productLine”]);

        BOOL written = [[d objectForKey:@"image"] [color=#FF0000]writeToFile:@"/tmp/image.jpg"[/color]

In that case, I’m always overwriting the same image.jpg file, but surely there is a way to create an image file bearing the name of the computer (i.e. my productLine) ?

Thanks for your hints
And thanks for this great book, I can’t leave it for more than a few hours in a row…


Complete code for main.c :

// main.m
// PropertyList
// Created by ADDA Frédéric on 14/05/12.
// Copyright © 2012 MyCompanyName. All rights reserved.

#import <Foundation/Foundation.h>

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

@autoreleasepool {
    // Challenge for chapter 26 "Property lists"
    // Objective : create a property list for a list of computers
    NSError *error = nil;
    // Images
    NSData *macBookPro = [NSData dataWithContentsOfFile: @"/Users/Fred/Pictures/Divers/Images Apple/Appareils Apple/MacBook Pro early 2011.jpg"];
    NSData *iMac = [NSData dataWithContentsOfFile: @"/Users/Fred/Pictures/Divers/Images Apple/Appareils Apple/iMac G4.jpg"];
    NSMutableArray *computersList = [[NSMutableArray alloc] init];
    NSMutableDictionary *computer;
    // P-list entry for MacBookPro 13"
    computer = [NSMutableDictionary dictionary];
    [computer setObject:@"MacBook Pro" forKey:@"productLine"];
    [computer setObject:[NSNumber numberWithInt:13] forKey:@"screenSizeInInches"];
    [computer setObject:[NSDate dateWithString:@"2011-03-20 12:00:00 +0100"] forKey:@"dateOfPurchase"];
    [computer setObject:[NSNumber numberWithFloat:2.3] forKey:@"processorSpeedInGhz"];
    [computer setObject:[NSNumber numberWithBool:YES] forKey:@"SSDDrive"];
    [computer setObject:macBookPro forKey:@"image"];
    [computersList addObject];
    // P-list Entry for iMac G4
    computer = [NSMutableDictionary dictionary];
    [computer setObject:@"iMac G4" forKey:@"productLine"];
    [computer setObject:[NSNumber numberWithInt:15] forKey:@"screenSizeInInches"];
    [computer setObject:[NSDate dateWithString:@"2002-06-26 12:00:00 +0100"] forKey:@"dateOfPurchase"];
    [computer setObject:[NSNumber numberWithFloat:1.0] forKey:@"processorSpeedInGhz"];
    [computer setObject:[NSNumber numberWithBool:NO] forKey:@"SSDDrive"];
    [computer setObject:iMac forKey:@"image"];
    [computersList addObject];
    [computersList writeToFile:@"/tmp/computers.plist"
    NSArray *localPropertyList = [NSArray arrayWithContentsOfFile:@"/tmp/computers.plist"];
    for (NSDictionary *d in localPropertyList) {
        NSLog(@"I have a %@",[d objectForKey:@"productLine"]);
        BOOL written = [[d objectForKey:@"image"] writeToFile:@"/tmp/image.jpg"
            NSLog(@"Write failed for image of %@",[d objectForKey:@"productLine"]);
            return 1;
        } else {
            NSLog(@"Image for %@ written to disk", [d objectForKey:@"productLine"]);
return 0;