About Portfolio challenge


#1

Hi all,
I was completing the Portfolio challenge and I have a doubt about an “if” in a method that add “stocks” in “listPortfolio” array:

  • if I create the array with this line of code, the array will be not populated by stocks

                       NSMutableArray *listPortfolio = [[NSMutableArray alloc]init];
    

    while if i put this one, the array will be filled with stocks

                       listPortfolio = [[NSMutableArray alloc]init];
    

I don’t understand why…furthermore Xcode tell me that the variable listPorfolio is not used:
Someone could help me to understand?

Thanks

P.S. Below my code for the challenge. I’m using Xcode 4.2 on Lion

Portfolio.m

[code]
#import “Portfolio.h”
#import “StockHoldings.h”

@implementation Portfolio

@synthesize portfolioValue;

//method addtoportfolio
-(void)addToPortfolio:(StockHoldings *)holding
{
if (!listPortfolio) {

    //listPortfolio = [[NSMutableArray alloc]init]; This is good *************************
    //Why this is bad ***************************************
    NSMutableArray *listPortfolio = [[NSMutableArray alloc]init];
}
[listPortfolio addObject:holding];

}

@end[/code]

Portfolio.h

[code]#import <Foundation/Foundation.h>
@class StockHoldings;

@interface Portfolio : NSObject
{
float portfolioValue;
NSMutableArray *listPortfolio;
}

@property float portfolioValue;

//method to add stocks in listportfolio

  • (void)addToPortfolio:(StockHoldings *)holding;

@end[/code]

main.m

[code]#import <Foundation/Foundation.h>
#import “StockHoldings.h”
#import “Portfolio.h”

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

@autoreleasepool {
    
    //Create an array
    NSMutableArray *myStocks = [NSMutableArray array];
                                
    //Create instance of StockHolding
    StockHoldings *Apple = [[StockHoldings alloc]init];
    StockHoldings *Samsung = [[StockHoldings alloc]init];
    StockHoldings *Microsoft = [[StockHoldings alloc]init];
                                                            
    //Add value to instance of StockHolding with accessor methods
    [Apple setPurchaseSharePrice:2.30];
    [Apple setCurrentSharePrice:4.50]; 
    [Apple setNumberOfShares:40];

    [Samsung setPurchaseSharePrice:12.19];
    [Samsung setCurrentSharePrice:10.56]; 
    [Samsung setNumberOfShares:90];
                                
    [Microsoft setPurchaseSharePrice:45.10];
    [Microsoft setCurrentSharePrice:49.51]; 
    [Microsoft setNumberOfShares:210];
    
    //add instance of stockholdings
    [myStocks addObject:Apple];
    [myStocks addObject:Samsung];
    [myStocks addObject:Microsoft];
    
    //create object Portfolio
    Portfolio *portfolio1 = [[Portfolio alloc]init];
    
    for (StockHoldings *st in myStocks){
        [portfolio1 addToPortfolio:st];

        float currentValue; //set variable currentValue

        currentValue += [st valueInDollars]; //set current value to have sum of valueInDollars for every stock

        [portfolio1 setPortfolioValue:currentValue];//set portfolio1 value to be currentValue
    }
    
    NSLog(@"The total value of my Portfolio is %f",[portfolio1 portfolioValue]);
}
return 0;

}[/code]


#2

Before I explain why, here’s an illustration of why:

int a = 5;

if (/* some truth value */) {
    int a = 3;
}

if (a == 5) {
    NSLog(@"This statement will appear in your console");
}

Because listPortfolio is defined as an ivar (instance variable), it already exists (uninitialized) when addToPortfolio: is invoked. When you add NSMutableArray * in front of it, you’re declaring a local variable with the same name that will vanish as soon as the method is completed.


#3

Thanks Macintux for your prompt reply.
Now I understand why Xcode told me that the variable is unused…

Maybe it could be strange but this chapter (and especially from the Asset part) was a little bit difficult and get me confused when I faced the challenge.

May I ask you, if in your opinion, the code I wrote reflect the challenge? I mean about the relationship between the objects.

Thanks again.


#4

Your code looks like a straightforward solution, yes. You’re on the right track.