My code to the StockHolding Challenge!


#1
//main.m
 NSMutableArray *stockArray=[[NSMutableArray alloc] init ];
                                    
        for(int i=1;i<4;i++)
        {
            
            StockHolding *s=[[StockHolding alloc] init];
            s.purchaseSharePrice=2.3*i;
            s.currentSharePrice=5.8*i;
            s.numberOfShares=54*i;
            
            [stockArray addObject:s];
            
            
        NSLog(@"The value of %i stock is $%0.2f and it's cost is $%0.2f",
              i, [[stockArray objectAtIndex:i-1] valueInDollars],
              [[stockArray objectAtIndex:i-1] costInDollars]
              );
        }
        


//StockHolding.h////////////////////////////////////////
#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;    
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
-(float)costInDollars;
-(float)valueInDollars;
@end


//StockHolding.m////////////////////////////////////////
#import "StockHolding.h"

@implementation StockHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

-(float)costInDollars{
    return purchaseSharePrice*numberOfShares;
}
-(float)valueInDollars{
    return currentSharePrice*numberOfShares;
}
@end

I have the array stockArray outside the loop, the for loops makes an instance of the StockHolding object, sets the properties and adds them to the stockArray. The NSLog calls the methods(valueInDollars and costInDollars) on each object which returns a float.