Challenge Solution


#1

Took a stab at it, can be simplified no doubt.

//  main.m
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        NSMutableArray *theArray;
        NSMutableDictionary *theRoot = [NSMutableDictionary dictionaryWithCapacity:8];
        NSDictionary *theDictionary;
        NSString *theString;
        NSData *theData;
        NSDate *theDate;
        NSNumber *intNumber = [NSNumber numberWithInt:6];
        NSNumber *floatNumber = [NSNumber numberWithFloat:4.9];
        NSNumber *boolNumber = [NSNumber numberWithBool:TRUE];
        NSString *error;
        
        //First dictionary of an array, give it 3 things
        theData = [NSData dataWithContentsOfFile:@"/tmp/mark.txt"];
        theString = @"First string";
        theDate = [NSDate dateWithString:@"2011-11-15 02:25:00 +0600"];
        
        theArray = [NSMutableArray arrayWithObjects:theData,theString,theDate, nil];
        theDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:theData,theString,theDate,nil] 
                                                    forKeys:[NSArray arrayWithObjects:@"Data",@"String",@"Date", nil]];                                              
        [theRoot setObject:theDictionary forKey:@"FirstDictionaryArray"];
        
        //Second dictionary of an array, give it 3 things
        theData = [NSData dataWithContentsOfFile:@"/tmp/mark.txt"];
        theString = @"Second string";
        theDate = [NSDate dateWithString:@"2011-10-31 01:12:00 +0600"];
        
        theArray = [NSMutableArray arrayWithObjects:theData,theString,theDate, nil];
        theDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:theData,theString,theDate,nil] 
                                                    forKeys:[NSArray arrayWithObjects:@"Data",@"String",@"Date", nil]];                                              
        [theRoot setObject:theDictionary forKey:@"SecondDictionaryArray"];
        
        [theRoot setObject:theArray forKey:@"Array"];
        [theRoot setObject:theString forKey:@"String"];
        [theRoot setObject:theData forKey:@"Data"];
        [theRoot setObject:theDate forKey:@"Date"];
        [theRoot setObject:intNumber forKey:@"intNumber"];
        [theRoot setObject:floatNumber forKey:@"floatNumber"];
        [theRoot setObject:boolNumber forKey:@"boolNumber"];
        
        id plist = [NSPropertyListSerialization dataFromPropertyList:(id)theRoot
                    format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
        [plist writeToFile:@"/tmp/stuff.plist" atomically:TRUE];
    }
    return 0;
}

#2

Here is another example. Stores the following song information in a plist file /tmp/albums.plist

Album: Loud - (NSString)
Artist: Rihanna - (NSString)
Genre: Pop - (NSString)
Name: Only Girl (In the World) - (NSString)
Duration: 3 minutes 55 seconds - (int) stored as seconds
Release Date: 2011 - (NSDate)
Price: 1.25 - (float)
Explicit: NO - (bool)
Artwork: /Users/sa/Downloads/mzi.tzkiunun.170x170-75.jpg - (NSData)

// Testing: read from data and test
For testing, we read back the plist file after writing it. Then write the jpg artwork file as /tmp/rihanna-loud.jpg.

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        NSData *artwork = [[NSData alloc] initWithContentsOfFile:@"/Users/sa/Downloads/mzi.tzkiunun.170x170-75.jpg"];

        NSMutableDictionary *song = [[NSMutableDictionary alloc] init];
        [song setValue:@"Only Girl (In the World)" forKey:@"Name"];
        [song setValue:[NSNumber numberWithInt:3 * 60 + 55] forKey:@"Duration"];
        [song setValue:[NSDate dateWithNaturalLanguageString:@"2011"] forKey:@"Release Date"];
        [song setValue:[NSNumber numberWithFloat:1.25] forKey:@"Price"];
        [song setValue:[NSNumber numberWithBool:NO] forKey:@"Explicit"];
        [song setValue:artwork forKey:@"Artwork"];

        NSMutableDictionary *album = [[NSMutableDictionary alloc] init];
        [album setValue:@"Loud" forKey:@"Album"];
        [album setValue:@"Rihanna" forKey:@"Artist"];
        [album setValue:@"Pop" forKey:@"Genre"];
        [album setValue:song forKey:@"Song"];
        
        NSMutableArray *albums = [[NSMutableArray alloc] init];        
        [albums addObject:album];
        
        [albums writeToFile:@"/tmp/albums.plist" atomically:YES];
        
        // Testing: read from data and test
        NSArray *albumsArray = [[NSArray alloc] initWithContentsOfFile:@"/tmp/albums.plist"];
        NSMutableDictionary *albumDict = [[NSMutableDictionary alloc] init];
        albumDict = [albumsArray objectAtIndex:0];
        NSMutableDictionary *songDict = [[NSMutableDictionary alloc] init];
        songDict = [albumDict valueForKey:@"Song"];
        
        NSData *artworkData = [songDict valueForKey:@"Artwork"];
        [artworkData writeToFile:@"/tmp/rihanna-loud.jpg" atomically:YES];
        
    }
    return 0;
}

#3

My version below.

The omitted adding NSData.

//
//  main.m
//  properListChallenge
//
//  Created by Kurt Farao on 2012/02/09.
//  Copyright (c) 2012 kurt_farao@yahoo.com. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // create date objects
        NSDate *today = [NSDate date];
        NSDate *tomorrow = [today dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
        NSDate *yesterday = [today dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];
        
        // create dateformat
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        
        // set date
        [formatter setDateFormat:@"yyyy-MM-dd"];
        
        // get date strings
        NSString *dateToday = [formatter stringFromDate:today];
        NSString *dateTomorrow = [formatter stringFromDate];
        __unused NSString *dateYesterday = [formatter stringFromDate:yesterday];
        
        
        // create dictionary
        NSMutableArray *garage = [[NSMutableArray alloc] init];
        
        NSMutableDictionary *cars;
        
        // dataset 1
        cars = [NSMutableDictionary dictionary];
        [cars setObject:dateToday
                 forKey:@"date"];
        [cars setObject:@"mustang"
                 forKey:@"car"];
        [cars setObject:[NSNumber numberWithInt:4592]
                 forKey:@"id"];
        [cars setObject:[NSNumber numberWithFloat:867.79]
                 forKey:@"weight(kg)"];
        [cars setObject:[NSNumber numberWithBool:TRUE]
                 forKey:@"insurance"];
        [garage addObject:cars];
        
        // dataset 2
        cars = [NSMutableDictionary dictionary];
        [cars setObject:dateTomorrow
                 forKey:@"date"];
        [cars setObject:@"porsche"
                 forKey:@"car"];
        [cars setObject:[NSNumber numberWithInt:6678]
                 forKey:@"id"];
        [cars setObject:[NSNumber numberWithFloat:123.89]
                 forKey:@"weight(kg)"];
        [cars setObject:[NSNumber numberWithBool:FALSE]
                 forKey:@"insurance"];
        [garage addObject:cars];
         
         
        [garage writeToFile:@"/tmp/car.plist"
                 atomically:YES];
    }
    return 0;
}


#4

Well heres another version. Pretty straight forward. Just Make sure there is that google logo in your temp folder from chapter 23.

[code]#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    // A muttable array to hold the eight types of property lists
    NSMutableArray *eightTypesOfP = [[NSMutableArray alloc] init];
    
    
    // Type00: Array
    // Create an instance of NSArray named array and add the objects to it
    // Then add it to the eightTypesOfP
    NSArray *typeArray =[NSArray arrayWithObjects:@"Zero", @"One", @"Two", @"Three", @"Four", @"Five", nil];
    
    [eightTypesOfP addObject:typeArray];
    
    // Type01: Dictionary
    // Create an instance of NSDictionary named dictionary and add a some objects and keys to it
    // Then add it to the eightTypesOfP
    NSDictionary *typeDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Charles", @"nameFirst", @"Wright", @"nameMiddle", @"Mills", @"nameLast", nil];
    
    [eightTypesOfP addObject:typeDictionary];
    
    // Type02: String
    // Create an instance of NSSting and write something in it
    // Then add it to the eightTypesOfP
    NSString *typeSting = @"This is a string type";
    
    [eightTypesOfP addObject:typeSting];
    
    // Type03: Data
    // Make sure there is a file /tmp/google.png from the chapter 23 work through
    // Create an instance of NSData with contents of above file
    // Then add it to the eightTypesOfP
    NSData *typeData = [NSData dataWithContentsOfFile:@"/tmp/google.png"];
    
    [eightTypesOfP addObject];
    
    // Type04: Date
    // Yoou should get the point by now
    NSDate *typeDate = [NSDate date];
    
    [eightTypesOfP addObject];
    
    
    // Type05: Integer
    NSNumber *typeInteger = [NSNumber numberWithInt:10];
    
    [eightTypesOfP addObject:typeInteger];
    
    // type06: float
    NSNumber *typeFloat = [NSNumber numberWithFloat:10.50];
    
    [eightTypesOfP addObject:typeFloat];
    
    //type07: boolean
    NSNumber *typeBoolean = [NSNumber numberWithBool:1];
    
    [eightTypesOfP addObject:typeBoolean];
    
    // Write eightTypesOfP to file
    [eightTypesOfP writeToFile:@"/tmp/eightTypesOfP.plist"
                    atomically:YES];
    
}
return 0;

}[/code]


#5

Here is my version of the solution. I kept it very bland. Again you’ll need to make sure you have cool.txt and google.png in your tmp drive.

main.m

[code]//
// main.m
// Challenge26
//
// Created by Mark Bridges on 14/05/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

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

@autoreleasepool {
    // Create NSMutableArray to hold data types
    NSMutableArray *pListWithAllTypes = [[NSMutableArray alloc] init ];
    
    // Create pointer to NSMutableDictionary
    NSMutableDictionary *aDataType;
    
    // Add an array and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSArray arrayWithObjects:@"FirstObject", @"SecondObject", @"ThirdObject", nil]
                  forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"Array"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Add a dictionary and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"FirstDictionaryObject", @"SecondDictionaryObject", @"ThirdDictionaryObject", nil] forKeys:[NSArray arrayWithObjects:@"FirstKey", @"SecondKey", @"ThirdKey", nil ]] forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"Dictionary"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Add a string and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSString stringWithContentsOfFile:@"/tmp/cool.txt"
                                                   encoding: NSUTF8StringEncoding
                                                      error:NULL]
          forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"String"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Add data and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSData dataWithContentsOfFile:@"/tmp/google.png"]
          forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"Data"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Add a date and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSDate dateWithString:@"2012-02-20 12:30:30 +0000"]
          forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"Date"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Add an integer and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSNumber numberWithInt:5.0]
          forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"Integer"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Add a float and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSNumber numberWithFloat:4.55]
          forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"Float"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Add a BOOL and add it to the NSMutableArray
    aDataType = [NSMutableDictionary dictionary];
    [aDataType setObject:[NSNumber numberWithBool:YES]
          forKey:@"TheData"];
    [aDataType setObject:[NSString stringWithString:@"BOOL"]
                  forKey:@"DataTypeDescription"];
    [pListWithAllTypes addObject:aDataType];
    
    // Write the contents of the NSMutableArray to a plist file
    [pListWithAllTypes writeToFile:@"/tmp/DataTypes.plist"
                        atomically:YES];
    
}
return 0;

}

[/code]


#6

I just used the stockz app as a basis since it already had NSArray, NSDictionary, a string and an int in it. Just had to add the other types.

[code]#import <Foundation/Foundation.h>

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

@autoreleasepool {
    //We have to include an array, dictionary, string, data, date, integer, float, and boolean in our plist. 
    //Comments will be used to ID the type
    
    //Array
    NSMutableArray *stocks = [[NSMutableArray alloc] init];
    
    //Dictionary
    NSMutableDictionary *stock;
    
    stock = [NSMutableDictionary dictionary];
    
    //String
    [stock setObject:@"AAPL" forKey:@"symbol"];
    
    //Integer
    [stock setObject:[NSNumber numberWithInt:200] forKey:@"shares"];
    
    //Date 
    [stock setObject:[NSDate date] forKey:@"date"];
    
    //Float
    [stock setObject:[NSNumber numberWithFloat:32.33] forKey:@"price"];
    
    //Boolean
    [stock setObject:[NSNumber numberWithBool:YES] forKey:@"settled"];
    
    //Data
    NSData *pic = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com/images/logos/ps_logo2.png"]];
    [stock setObject:pic forKey:@"logo"];
    
    [stocks addObject:stock];
    
    stock = [NSMutableDictionary dictionary];
    [stock setObject:@"GOOG" forKey:@"symbol"];
    [stock setObject:[NSNumber numberWithInt:160] forKey:@"shares"];
    [stock setObject:[NSDate date] forKey:@"date"];
    [stock setObject:[NSNumber numberWithFloat:12.45] forKey:@"price"];
    [stock setObject:[NSNumber numberWithBool:NO] forKey:@"settled"];
    NSData *pic1 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com/images/logos/ps_logo2.png"]];
    [stock setObject:pic1 forKey:@"logo"];
    [stocks addObject:stock];
    [stocks writeToFile:@"/tmp/stocks.plist" atomically:YES];

}
return 0;

}[/code]


#7

I made a RecipeBook class.
To make it easier to see this is what my solution looks like:

"RecipeBook(subclass of NSObject)"
Instance variable: NSMutableDictionary *recipe

  • key: food name (NSString) / object: recipe (NSMutableArray)

index[0]: NSMutableDictionary *ingredients

  • key: ingredient (NSString) / object: amount (NSNumber)
    index[1] ~ [n-2]: Steps (NSString)
    index[n-1]: Picture (NSData)
    index[n]: Modified Date (NSDate)

RecipeBook.h

#import <Foundation/Foundation.h>

@interface RecipeBook : NSObject
{
    NSMutableDictionary *recipe;
}

-(void)setKeyword:(NSString *)key;
-(void)addIngredient:(NSNumber *)amount of:(NSString*)ingredient toRecipe:(NSString *)key;
-(void)addStep:(NSString *)step toRecipe:(NSString *)key;
-(void)addStep:(NSString *)step atStep:(NSUInteger)index toRecipe:(NSString *)key;
-(void)addPictureAtUrl:(NSString *)url toRecipe:(NSString *)key;
-(void)modifyUploadDateTo:(NSDate *)date ofRecipe:(NSString *)key;

-(void)printListOfIngredientsFor:(NSString *)key;
-(void)printRecipe:(NSString *)key;
-(void)exportToPlist;

@end

RecipeBook.m

#import "RecipeBook.h"
#define RECIPE(key) [recipe objectForKey:key]
#define INGREDIENTS(key) [RECIPE(key) objectAtIndex:0]

@implementation RecipeBook

// Keyword for Recipe
-(void)setKeyword:(NSString *)key
{
    if(!recipe)
        recipe = [NSMutableDictionary dictionary];
    [recipe setObject:[NSMutableArray array] forKey:key];
}

// Add ingredient to recipe
-(void)addIngredient:(NSNumber *)amount of:(NSString*)ingredient toRecipe:(NSString *)key
{
    if(![RECIPE(key) count])
        [RECIPE(key) addObject:[NSMutableDictionary dictionary]];
    
    [INGREDIENTS(key) setObject:amount forKey:ingredient];
}

// Add step to recipe
-(void)addStep:(NSString *)step toRecipe:(NSString *)key
{
    [RECIPE(key) addObject:step];
}

// Add step in the middle of recipe
-(void)addStep:(NSString *)step atStep:(NSUInteger)index toRecipe:(NSString *)key
{
    [RECIPE(key) insertObject:step atIndex:index];
}

// Add picture (and date) to recipe
-(void)addPictureAtUrl:(NSString *)url toRecipe:(NSString *)key
{
    NSURL *picUrl = [NSURL URLWithString:url];
    NSError *error = nil;
    NSURLRequest *request = [NSURLRequest requestWithURL:picUrl];
    NSData *picture = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:&error];
    
    if (!picture) {
        NSLog(@"Picture load falied: %@", [error localizedDescription]);
        [RECIPE(key) addObject:[NSDate date]];
        return;
    }
    else {
        [picture writeToFile:@"/tmp/recipe.jpg" options:NSDataWritingAtomic error:&error];
        [RECIPE(key) addObject:picture];
        [RECIPE(key) addObject:[NSDate date]];
    }

}

// Modify upload date
-(void)modifyUploadDateTo:(NSDate *)date ofRecipe:(NSString *)key
{
    [RECIPE(key) replaceObjectAtIndex:([RECIPE(key) count] - 1) withObject:date];
}

// Print all ingredients
-(void)printListOfIngredientsFor:(NSString *)key
{
    [INGREDIENTS(key) enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *obj, BOOL *stop) {
        NSLog(@"%@: %@", key, obj);
    }];
}

// Print complete recipe
-(void)printRecipe:(NSString *)key
{
    for (int i = 0; i < [RECIPE(key) count]; i++) {
        if (i == 0)
            [self printListOfIngredientsFor:key];
        else if (i == ([RECIPE(key) count] - 2))
            NSLog(@"Picture saved at /tmp/picture.jpg (%lu bytes)", [[RECIPE(key) objectAtIndex:i] length]);
        else if (i == ([RECIPE(key) count] - 1))
            NSLog(@"Recipe written on %@", [RECIPE(key) objectAtIndex:i]);
        else
            NSLog(@"Step %i: %@", i, RECIPE(key)[i]);
    }
}

// Export to "recipe.plist"
-(void)exportToPlist
{
    [recipe writeToFile:@"/tmp/recipe.plist" atomically:YES];
    NSLog(@"Recipe exported to \"/tmp/recipe.plist\"");
}

@end

main.m

#import <Foundation/Foundation.h>
#import "RecipeBook.h"

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        RecipeBook *myRecipe = [[RecipeBook alloc]init];
        
        [myRecipe setKeyword:@"Apple Pie"];

        [myRecipe addIngredient:[NSNumber numberWithFloat:0.75] of:@"white sugar (C)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithInt:2] of:@"all-purpose flour (T)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithFloat:0.5] of:@"ground cinnamon (t)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithFloat:0.25] of:@"ground nutmeg (t)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithFloat:0.5] of:@"lemon zest (t)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithInt:7] of:@"thinly sliced apples (C)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithInt:2] of:@"lemon juice (t)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithInt:1] of:@"butter (T)" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithInt:1] of:@"pastery for a 9 inch double crust pie" toRecipe:@"Apple Pie"];
        [myRecipe addIngredient:[NSNumber numberWithInt:4] of:@"mile (T)" toRecipe:@"Apple Pie"];

        [myRecipe addStep:@"Preheat oven to 425 degrees F (220 degrees C)." toRecipe:@"Apple Pie"];
        [myRecipe addStep:@"Mix together the sugar, flour, cinnamon, nutmeg and lemon peel." toRecipe:@"Apple Pie"];
        [myRecipe addStep:@"Line one crust in a 9-inch deep-dish pie pan. Layer 1/3 of apples into pie crust. Sprinkle with sugar mixture and repeat until done. Sprinkle with lemon juice and dot with butter." toRecipe:@"Apple Pie"];
        [myRecipe addStep:@"Bake at 425 degrees F (220 degrees C) for 40 to 50 minutes." toRecipe:@"Apple Pie"];
        [myRecipe addStep:@"Place second pie crust on top of filling and flute the edges. Cut vents in top crust and brush with milk for a glazed appearance if desired." atStep:4 toRecipe:@"Apple Pie"];
        
        [myRecipe addPictureAtUrl:@"http://images.media-allrecipes.com/userphotos/250x250/00/94/34/943496.jpg" toRecipe:@"Apple Pie"];
    
        NSLog(@"Now printing list of ingredients...");
        [myRecipe printListOfIngredientsFor:@"Apple Pie"];
        
        printf("\n");
        
        NSLog(@"Now printing complete recipe...");
        [myRecipe printRecipe:@"Apple Pie"];
        
        [myRecipe exportToPlist];
        
    }
    return 0;
}

#8

Really like this solution. Well thought out.

[quote=“virtualapps”]Here is another example. Stores the following song information in a plist file /tmp/albums.plist

Album: Loud - (NSString)
Artist: Rihanna - (NSString)
Genre: Pop - (NSString)
Name: Only Girl (In the World) - (NSString)
Duration: 3 minutes 55 seconds - (int) stored as seconds
Release Date: 2011 - (NSDate)
Price: 1.25 - (float)
Explicit: NO - (bool)
Artwork: /Users/sa/Downloads/mzi.tzkiunun.170x170-75.jpg - (NSData)

// Testing: read from data and test
For testing, we read back the plist file after writing it. Then write the jpg artwork file as /tmp/rihanna-loud.jpg.

[code]
#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    NSData *artwork = [[NSData alloc] initWithContentsOfFile:@"/Users/sa/Downloads/mzi.tzkiunun.170x170-75.jpg"];

    NSMutableDictionary *song = [[NSMutableDictionary alloc] init];
    [song setValue:@"Only Girl (In the World)" forKey:@"Name"];
    [song setValue:[NSNumber numberWithInt:3 * 60 + 55] forKey:@"Duration"];
    [song setValue:[NSDate dateWithNaturalLanguageString:@"2011"] forKey:@"Release Date"];
    [song setValue:[NSNumber numberWithFloat:1.25] forKey:@"Price"];
    [song setValue:[NSNumber numberWithBool:NO] forKey:@"Explicit"];
    [song setValue:artwork forKey:@"Artwork"];

    NSMutableDictionary *album = [[NSMutableDictionary alloc] init];
    [album setValue:@"Loud" forKey:@"Album"];
    [album setValue:@"Rihanna" forKey:@"Artist"];
    [album setValue:@"Pop" forKey:@"Genre"];
    [album setValue:song forKey:@"Song"];
    
    NSMutableArray *albums = [[NSMutableArray alloc] init];        
    [albums addObject:album];
    
    [albums writeToFile:@"/tmp/albums.plist" atomically:YES];
    
    // Testing: read from data and test
    NSArray *albumsArray = [[NSArray alloc] initWithContentsOfFile:@"/tmp/albums.plist"];
    NSMutableDictionary *albumDict = [[NSMutableDictionary alloc] init];
    albumDict = [albumsArray objectAtIndex:0];
    NSMutableDictionary *songDict = [[NSMutableDictionary alloc] init];
    songDict = [albumDict valueForKey:@"Song"];
    
    NSData *artworkData = [songDict valueForKey:@"Artwork"];
    [artworkData writeToFile:@"/tmp/rihanna-loud.jpg" atomically:YES];
    
}
return 0;

}
[/code][/quote]