Error in challenge


#1

ok, so this is my try at the challenge:

Portfolio.h

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

@interface Portfolio : StockHolding

{
NSMutableArray *labelCompra;
}

-(void)addLabelCompra:(Portfolio *)a;
-(float)valorPortfoio;

@end
[/code]

Portfolio.m

#import "Portfolio.h"
#import "StockHolding.h"

@implementation Portfolio

-(void)addLabelCompra:(Portfolio *)a
{
    if(!portfolio){
        portfolio = [[NSMutableArray alloc] init];
    }
    [portfolio addObject:a];
}

-(float)valorPortfoio
{
    double sum = 0;
    for (Portfolio *a in portfolio){
        sum += [a valueInDollars];
    }
    return sum;
}


@end[/code]

and main.m

[code]#import <Foundation/Foundation.h>
#import "ForeingStockHolding.h"
#import "Portfolio.h"

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

    @autoreleasepool {
        
        
    
        // crear tres instancias de strockholding
        StockHolding *primeraCompra = [[StockHolding alloc] init];
        
        // añadir valores
        [primeraCompra setPurchaseSharedPrice:2.30];
        [primeraCompra setCurrentSharePrice:4.50];
        [primeraCompra setNumberOfShares:40];
        
        
        StockHolding *segundaCompra = [[StockHolding alloc] init];
        
        [segundaCompra setPurchaseSharedPrice:12.19];
        [segundaCompra setCurrentSharePrice:10.56];
        [segundaCompra setNumberOfShares:90];
        
        
        ForeingStockHolding *terceraCompra = [[ForeingStockHolding alloc] init];
        
        [terceraCompra setPurchaseSharedPrice:45.10];
        [terceraCompra setCurrentSharePrice:49.51];
        [terceraCompra setNumberOfShares:210];
        [terceraCompra setConversionRate:0.95];
        
        
        
        
        NSMutableArray *compras = [NSMutableArray arrayWithObjects:primeraCompra, segundaCompra, terceraCompra, nil];
        
        for (ForeingStockHolding *d in compras) {
            NSLog(@"Para el portafolio, coste: %.2f y valor: %.2f", [d costInDollars], [d valueInDollars]);
   
            
        Portfolio *portofolio = [[Portfolio alloc] init];
            [portofolio addLabelCompra:primeraCompra];
            [portofolio addLabelCompra:segundaCompra];
            [portofolio addLabelCompra:terceraCompra];
            
            float precioFinal = [portofolio valorPortfoio];
            
            NSLog(@"precio final %f", precioFinal);
            
            }
            
    }
    return 0;
}

It works fine but I get this warnings:
Incompatible pointer types sending ‘StockHolding *__strong’ to parameter of type ‘Portfolio *’

on each line for this part of the code of main.m:

        [portofolio addLabelCompra:primeraCompra];
        [portofolio addLabelCompra:segundaCompra];
        [portofolio addLabelCompra:terceraCompra];

…can anyone tell why is this happening?..thanks!!


#2

The critical bit here is to remember that Portfolio is a subclass of StockHolding.

Any method that takes an instance of a StockHolding as an argument would be happy taking a Portfolio instead, because every Portfolio is also a StockHolding. This is what it means to be a subclass.

A method that takes an instance of a Portfolio, however, is less enthusiastic about being passed an instance of a StockHolding, because NOT every StockHolding is a Portfolio.

It’s like geometry: a square is a kind of rectangle, so every square is a rectangle but not every rectangle is square.

You’ve declared primeraCompra to be of type StockHolding, but you’re passing it into a method that’s expecting a Portfolio.