Challenge Question Before I Commence


#1

The challenge is asking to create a plist that contains all eight types listed.

Am I correct in thinking that the code we wrote during this chapter created a plist that contained the dictionary, the string and the integer such that all we are left to do is add data, date, float and boolean?

If this is the case then I may extend my Portfolio Stock challenge from chapter 19 to write to a plist with the additional types. Is this a good idea?


#2

That is correct.

[quote]If this is the case then I may extend my Portfolio Stock challenge from chapter 19 to write to a plist with the additional types. Is this a good idea?
[/quote]

I think that would be really illustrative. Yes, it is a good idea. (I wish I had thought of it!)


#3

Thanks, I am an excellent thinker but currently not very good at the implementation part. :sunglasses:


#4

How do you merge the Portflio and Stock into the Dictionary for inclusion into the plist?

My Main:

[code]//
// main.m
// Stocks
//
// Created by Paul Symonds on 14/02/2012.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

// add data, date, float and boolean… plist

#import <Foundation/Foundation.h>
#import “ForeignStockHolding.h”
#import “Portfolio.h”

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

@autoreleasepool {
    
    NSDate *now = [NSDate date];
   
    // Create 3 instances of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    
    //Create 2 instances of ForeignStockHolding
    ForeignStockHolding *stockholding4 =  [[ForeignStockHolding alloc] init];
    ForeignStockHolding *stockholding5 =  [[ForeignStockHolding alloc] init];
    
    // Create 3 Instances of Portfolio
    Portfolio *portfolio1 = [[Portfolio alloc] init];
    Portfolio *portfolio2 = [[Portfolio alloc] init];
    Portfolio *portfolio3 = [[Portfolio alloc] init];
    
    // Give the Portfolio a name
    [portfolio1 setPorfolioName:@"Blue"];
    [portfolio1 setIsActive:YES];
    [portfolio2 setPorfolioName:@"Red"];
    [portfolio2 setIsActive:YES];
    [portfolio3 setPorfolioName:@"Green"];
    [portfolio3 setIsActive:NO];
    
    // Add the stock holdings
    [portfolio1 addStockHoldingsObject:stockholding1];
    [portfolio1 addStockHoldingsObject:stockholding2];
    
    [portfolio2 addStockHoldingsObject:stockholding3];
    [portfolio2 addStockHoldingsObject:stockholding4];
    
    [portfolio3 addStockHoldingsObject:stockholding5];
    
    
    // Create an empty array
    //NSMutableArray *stockList = [NSMutableArray array];
    
    // Give the instance variables interesting values
    [stockholding1 setStockName:@"BAML"];
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    [stockholding1 setPurchaseDate:now];
    //[stockList addObject:stockholding1];
            
    // Give the instance variables interesting values
    [stockholding2 setStockName:@"BARCAP"];
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    [stockholding2 setPurchaseDate:now];
    
    
    //[stockList addObject:stockholding2];
    
    // Give the instance variables interesting values
    [stockholding3 setStockName:@"UKCL"];
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    [stockholding3 setPurchaseDate:now];
    
    // Give the instance variables interesting values
    [stockholding4 setStockName:@"SPPW"];
    [stockholding4 setPurchaseSharePrice:10.50];
    [stockholding4 setCurrentSharePrice:150.00];
    [stockholding4 setNumberOfShares:100];
    [stockholding4 setConversionRate:1.00];
    [stockholding4 setPurchaseDate:now];
    
    // Give the instance variables interesting values
    [stockholding5 setStockName:@"BBC"];
    [stockholding5 setPurchaseSharePrice:20.00];
    [stockholding5 setCurrentSharePrice:10.00];
    [stockholding5 setNumberOfShares:100];
    [stockholding5 setConversionRate:2.00];
    [stockholding5 setPurchaseDate:now];
    
    //[stockList addObject:stockholding3];
    
    // Create an array with objects
    NSMutableArray *stockList = [NSMutableArray arrayWithObjects:stockholding1, stockholding2, stockholding3, stockholding4, stockholding5, nil];
    
    
    NSMutableArray *portfolioList = [NSMutableArray arrayWithObjects:portfolio1, portfolio2, portfolio3, nil];
        
   
    // Call the stockholding methods
    
    for(StockHolding *n in stockList)
        {
        //Call the methods 
        float cost = [n costInDollars];
        float value = [n valueInDollars];
        NSLog(@"Bought stock %@ on %@ for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
             [n stockName], [n purchaseDate], [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
    }
    
    for(Portfolio *p in portfolioList)
    {
        //Call the methods 
        float portfolioValue = [p valueOfStockHoldings];
        NSLog(@"Active: %d - Current Value of portfolio %@ is $%.2f",
              [p isActive], [p porfolioName], portfolioValue);
    }
}
return 0;

}[/code]

Based on the example in the book for writing to the plist file below, am I correct in thinking that I would need to create a dictionary called *stockPorfolio and iterate though the portfolioList and with an inner loop iterate through the stockList to populate the dictionary object *stockPortfolio then finally write stockPortfolio to file?

        NSMutableArray *stocks = [[NSMutableArray alloc] init];
        
        
        
        NSMutableDictionary *stock;
        
        stock = [NSMutableDictionary dictionary];
        [stock setObject:@"APPL"
                  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];
        
        [stocks writeToFile:@"/tmp/stocks.plist"
                 atomically:YES];