Challenge complete, but warning recieved


#1

I think I’ve got the challenge complete, but I’m getting a “Semantic Issue: ‘NSArray’ may not respond to ‘valueInDollars’” in my Portfolio class that I’m not sure how to resolve:

Portfolio.m:

#import "Portfolio.h"

@implementation Portfolio

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


- (float)currentValue {
    float value;
    
    for (NSArray *x in stockList) {
        value += [x valueInDollars];  //error occurs here
    }
     
    return value;    
}

@end

I was thinking that the FOR loop doesn’t know the stockList exists yet… ?

Portfolio.h

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


@interface Portfolio : NSObject
{
    NSMutableArray *stockList;
}

- (void)addStockHoldingObject:(StockHolding *)s;
- (float)currentValue; // num socks * current price

@end

#2

You meant:

for (StockHolding *x in stockList) { value += [x valueInDollars]; }

While stockList is an NSArray, x will point to an individual item, in this case a StockHolding.


#3

Ahhh! lightbulb!

Thank you :slight_smile:

[quote=“AaronHillegass”]You meant:

for (StockHolding *x in stockList) { value += [x valueInDollars]; }

While stockList is an NSArray, x will point to an individual item, in this case a StockHolding.[/quote]