Error "No visible @interface..."


#1

OK, another try at the challenge and I’m still getting one error. The error is in main.m on the line at the end that says: NSLog(@“Value is %.2f", [stocks currentValue]); The complete error is "No visible @interface in NSMutable array declares the selector currentValue." So I changed the line of code to: NSLog(@"Value is %.2f”, [currentValue]) and I get an error that says: "Use of unidentified selector currentValue."
I don’t understand why I’m getting an error because I’ve declared currentValue in the .h file and implemented it in the .m file. In fact, if I just paste the implementation code from currentValue into main.m and comment out the line that was giving me the error, the program runs fine.
Once again, I’d be really grateful for any help. All my code is below. Thanks much. --Ed

[code]//
// Portfolio.h
//

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

@interface Portfolio : StockHolding
{
NSMutableArray *stocks;
}

@property NSMutableArray *stocks;

-(float) currentValue;

-(void)addObjects:(StockHolding *)a;

@end[/code]

[code]//
// Portfolio.m
//

#import “Portfolio.h”
#import “StockHolding.h”

@implementation Portfolio
@synthesize stocks;

-(float) currentValue
{
float sum = 0.0;
for (int i = 0; i < [stocks count]; i++) {
sum += [[stocks objectAtIndex:i] valueInDollars];
// NSLog(@"%f", sum);
}
return sum;
}

-(void)addObjects:(StockHolding *)a
{
[stocks addObjectsFromArray:(NSArray *)a];
}

@end[/code]

//
//  StockHolding.h
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

@property  float purchasePrice;
@property float currentPrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;


@end[/code]
[code]//
//  StockHolding.m
//

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchasePrice, currentPrice, numberOfShares;

-(float)costInDollars
{
    return (purchasePrice * numberOfShares);
}

-(float)valueInDollars
{
    return (currentPrice * numberOfShares);
}

@end[/code]
[code]//
//  main.m
//

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


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

    @autoreleasepool {
        
        StockHolding *edStock = [[StockHolding alloc]init];
        StockHolding *ermaStock = [[StockHolding alloc]init];
        StockHolding *boscoeStock = [[StockHolding alloc]init];
                
        [edStock setPurchasePrice:2.30];
        [edStock setCurrentPrice:4.50];
        [edStock setNumberOfShares:40];
        
        
        [ermaStock setPurchasePrice:12.19];
        [ermaStock setCurrentPrice:10.56];
        [ermaStock setNumberOfShares:90];
        
        
        [boscoeStock setPurchasePrice:45.10];
        [boscoeStock setCurrentPrice:49.51];
        [boscoeStock setNumberOfShares:210];
       
        
        NSMutableArray *stocks = [[NSMutableArray alloc] init];
        [stocks addObject:edStock];
        [stocks addObject:ermaStock];
        [stocks addObject:boscoeStock];
                      
       NSLog(@"Value is $%.2f", [stocks currentValue]);
    }
    
    
    return 0;
}

#2

There are two problems. I will show you one of them, but you need to discover the other one yourself so that real learning takes place.

First change this piece of code:

        NSMutableArray *stocks = [[NSMutableArray alloc] init];
        [stocks addObject:edStock];
        [stocks addObject:ermaStock];
        [stocks addObject:boscoeStock];
                      
        NSLog (@"Value is $%.2f", [stocks currentValue]);

To this:

        Portfolio *portfolio = [[Portfolio alloc] init];
        [[portfolio stocks] addObject:edStock];
        [[portfolio stocks] addObject:ermaStock];
        [[portfolio stocks] addObject:boscoeStock];
        
        NSLog (@"Value is $%.2f", [portfolio currentValue]);

Then find in the following jungle the new trees (that I have planted) that make the whole echo-system function:

//
//  StockHolding.h
//

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

@property  float purchasePrice;
@property float currentPrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;

@end


@implementation StockHolding

@synthesize purchasePrice, currentPrice, numberOfShares;

-(float)costInDollars
{
    return (purchasePrice * numberOfShares);
}

-(float)valueInDollars
{
    return (currentPrice * numberOfShares);
}

@end

//
//  Portfolio.h
//

@interface Portfolio : StockHolding

@property (strong) NSMutableArray *stocks;

-(float) currentValue;

-(void)addObjects:(StockHolding *)a;

@end

//
//  Portfolio.m
//

@implementation Portfolio
@synthesize stocks;

- (id)init
{
    self = [super init];
    self.stocks = [NSMutableArray array];
    return self;
}

-(float) currentValue
{
    float sum = 0.0;
    for (int i = 0; i < [stocks count]; i++) {
        sum += [[stocks objectAtIndex:i] valueInDollars];
        NSLog (@"---> %f", sum);
    }
    return sum;
}

-(void)addObjects:(StockHolding *)a
{
    [stocks addObjectsFromArray:(NSArray *)a];
}

//
//  main.m
//

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        
        StockHolding *edStock = [[StockHolding alloc]init];
        StockHolding *ermaStock = [[StockHolding alloc]init];
        StockHolding *boscoeStock = [[StockHolding alloc]init];
        
        [edStock setPurchasePrice:2.30];
        [edStock setCurrentPrice:4.50];
        [edStock setNumberOfShares:40];
        
        [ermaStock setPurchasePrice:12.19];
        [ermaStock setCurrentPrice:10.56];
        [ermaStock setNumberOfShares:90];
        
        [boscoeStock setPurchasePrice:45.10];
        [boscoeStock setCurrentPrice:49.51];
        [boscoeStock setNumberOfShares:210];
        
        Portfolio *portfolio = [[Portfolio alloc] init];
        [[portfolio stocks] addObject:edStock];
        [[portfolio stocks] addObject:ermaStock];
        [[portfolio stocks] addObject:boscoeStock];
        
        NSLog(@"Value is $%.2f", [portfolio currentValue]);
    }
    
    return 0;
}
@end

#3

Thanks very much – again – ibex10, for the answer and for the push in the right direction. I very much appreciate it.
–Ed