Now I know there are many solution, but i wanted to share mine with comments. I didnt use same amount of data in the array for the three options I had. The best thing about it is that doing this excersise actually made me understand the topic better!
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Create the main Array where everything is going to be stored
NSMutableArray *stockPortfolio = [[NSMutableArray alloc]init];
// Define a dictionary but not initialize it
NSMutableDictionary *stockDetails;
// Stock 1
stockDetails = [NSMutableDictionary dictionary]; // Initiliaze an empty dictionary
// String
[stockDetails setObject:@"AAPL"
forKey:@"Name"];
// Integer
[stockDetails setObject:[NSNumber numberWithInt:200]
forKey:@"Shares"];
// Float
[stockDetails setObject:[NSNumber numberWithFloat:20.3]
forKey:@"Price"];
// Date
[stockDetails setObject:[NSDate date]
forKey:@"Purchased at"];
// Array
[stockDetails setObject:[NSArray arrayWithObjects:@"Russia", @"Germany", nil]
forKey:@"Purchased in Countries"];
// Dictionary
// Provided the total value of bought stocks is in USD convert (manually) to local currency where the shares can be sold/bought
[stockDetails setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"253205.87", @"RUB", @"3571.45", @"EUR", nil]
forKey:@"Converted value"];
// Data
// In .plist file it will be a long line of characters and letters because the NSData returns an encoded answer
[stockDetails setObject:[NSData dataWithContentsOfFile:@"/tmp/stocks.plist"]
forKey:@"Stock file"];
// Boolean
[stockDetails setObject:[NSNumber numberWithBool:YES] forKey:@"Is the Stock purchased?"];
// Add all of the above to the main Array
[stockPortfolio addObject:stockDetails];
sleep(5); // Sleep so Purchased at date is different slightly for each Stock #
// Stock 2
stockDetails = [NSMutableDictionary dictionary];
[stockDetails setObject:@"GOOG"
forKey:@"Name"];
[stockDetails setObject:[NSNumber numberWithInt:100]
forKey:@"Shares"];
[stockDetails setObject:[NSNumber numberWithFloat:14.9]
forKey:@"Price"];
[stockDetails setObject:[NSDate date]
forKey:@"Purchased at"];
[stockDetails setObject:[NSArray arrayWithObjects:@"United Kingdom", nil]
forKey:@"Purchased in Countries"];
[stockDetails setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1103.75", @"GBP", nil]
forKey:@"Converted value"];
[stockDetails setObject:[NSNumber numberWithBool:NO] forKey:@"Is the Stock purchased?"];
[stockPortfolio addObject:stockDetails];
sleep(5);
// Stock 3
stockDetails = [NSMutableDictionary dictionary];
[stockDetails setObject:[NSString stringWithFormat:@"YHOO"]
forKey:@"Name"];
[stockDetails setObject:[NSNumber numberWithInt:50]
forKey:@"Shares"];
[stockDetails setObject:[NSNumber numberWithFloat:33.5]
forKey:@"Price"];
[stockDetails setObject:[NSDate date]
forKey:@"Purchased at"];
[stockDetails setObject:[NSArray arrayWithObjects:@"Netherlands", @"Russia", @"Japan", nil]
forKey:@"Purchased in Countries"];
[stockDetails setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1473.71", @"EUR", @"104381.23", @"RUB", @"200923.24", @"JPY", nil]
forKey:@"Converted value"];
[stockPortfolio addObject:stockDetails];
// Write the file to a .plis file
[stockPortfolio writeToFile:@"/tmp/stocks.plist"
atomically:YES];
}
return 0;
}[/code]