Challenge question


#1

I got the challenge to work, but then I tried to do a couple of extra things I thought would be simple to do, but they didn’t work and I’m not sure why. For example, I wanted to count the number of elements in my portfolio array. I then wanted to cycle through them and print out valueInDollars from each element. In both cases I got errors. Would appreciate any insight into this!

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”
@interface Portfolio : NSObject
{
NSMutableArray *portfolio;
float totalValue;
}

@property float totalValue;

-(void)addStock:(StockHolding *)s;
-(float)calculateTotalValue;

@end[/code]

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

@implementation Portfolio

@synthesize totalValue;

-(void)addStock:(StockHolding *)s
{
if (!portfolio) {
portfolio = [[NSMutableArray alloc] init];
}
[portfolio addObject:s];
}

-(float)calculateTotalValue
{
float sum = 0;
for (StockHolding *s in portfolio) {
sum += [s valueInDollars];
}
return sum;
}

@end[/code]

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “ForeignStockHolding.h”
#import "Portfolio.h"
int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    StockHolding *stock1 = [[StockHolding alloc] init];
    StockHolding *stock2 = [[StockHolding alloc] init];
    StockHolding *stock3 = [[StockHolding alloc] init];
    
    [stock1 setCurrentSharePrice: 22];
    [stock1 setPurchaseSharePrice: 15];
    [stock1 setNumberOfShares:1000];
    NSLog(@"%.2f", [stock1 valueInDollars]);
    
    [stock2 setCurrentSharePrice: 27];
    [stock2 setPurchaseSharePrice: 12];
    [stock2 setNumberOfShares:1500];
    NSLog(@"%.2f", [stock2 valueInDollars]);
   
    [stock3 setCurrentSharePrice: 33];
    [stock3 setPurchaseSharePrice: 18];
    [stock3 setNumberOfShares:1800];
    NSLog(@"%.2f", [stock3 valueInDollars]);
    
    Portfolio *portfolio1 = [[Portfolio alloc] init];
    
    [portfolio1 addStock: stock1];
    [portfolio1 addStock: stock2];
    [portfolio1 addStock: stock3];
   
    NSLog(@"%.2f", [portfolio1 calculateTotalValue]);
    
    // here I try to count and list, but it doesn't work
    
    NSLog(@"%.2f", [portfolio1 count]);
    
    for (StockHolding *x in portfolio1) {
        NSLog(@"%.2f", [x valueInDollars]);
    }
    
    
}
return 0;

}[/code]


#2

Putting under the microscope:

NSLog(@"%.2f", [portfolio1 count]);

for ([b]StockHolding[/b] *x in portfolio1) {
       NSLog(@"%.2f", [x valueInDollars]);
}

Reveals two problems.
Problem 1: [color=#FF0000][portfolio1 count][/color]
Portfolio class is not a collection class; therefore portfolio1 is not a collection object thus [portfolio1 count] is illegal.

Problem 2: for ([color=#FF0000]StockHolding *x in portfolio1[/color]) {
Again portfolio1 is not a collection object.

This is probably what you had in mind:

NSLog(@"%.2f", [[portfolio1 portfolios] count]);

for ([b]StockHolding[/b] *x in [portfolio1 portfolios]) {
       NSLog(@"%.2f", [x valueInDollars]);
}

Then the Portfolio class’s interface should look like this:

#import "StockHolding.h"
@interface Portfolio : NSObject
{
    NSMutableArray *portfolios;
    float totalValue;
}

@property float totalValue;

-(void)addStock:(StockHolding *)s;
-(float)calculateTotalValue;

- (NSArray *)portfolios;  // <--- implement this to return portfolios instance

#3

Thanks, super helpful! I got it now.


#4

Hey guys, I know this thread has been dead for a bit, but the problem within still applies to me.

I’m confused because I thought that Portfolios would be a collection class considering we created an NSMutableArray to take care of stuff in the methods/messages.

Once I realized (after reading this thread and getting so many SIGABRT errors) that portfolio ISNT a collection class, I tried to figure out how to make a custom constructor for Portfolios that would actually make a collections class, but I got stumped pretty quick.

Your hint, to implement -(NSArray *)portfolios; got me thinking about it, but I’m still confused as to how I’d implement that and return an instance of portfolio?