I have created two classes, one for StockHoldings, and the other Portfolio (an array). I created the values in main and am trying to add the created stock values into the portfolio, but when I do it I get “incompatible pointer types sending…”
This is what I have in main:
Portfolio *thisPortfolio = [[Portfolio alloc] init];
[thisPortfolio addPortfolioObject:[stockOne valueInDollars]];
Even if I just try and put an integer in place of [stockOne valueInDollars] which is a method of that calculates a stock’s value based off instance variables in stockholding, then I get “Implicit conversion of ‘int’ to ‘Portfolio’…”
This is what I have in the Portfolio.h file:
@interface Portfolio : NSObject
{
NSMutableArray *myPortfolio;
}
- (void)addPortfolioObject:(Portfolio *)a;
And this is what I have in the Portolfio.m file:
- (void)addPortfolioObject:(Portfolio *)a
{
// Is assets nil?
if (!myPortfolio) {
// Create the array
myPortfolio = [[NSMutableArray alloc] init];
}
[myPortfolio addObject:a];
}
What am I doing wrong?
In the interface of
Portfolio, indeed you declare the signature ofaddPortfolioObjectsuch that aPortfolioobject is expected, not an integer. The signature will have to change to(void)addPortfolioObject:(int)aboth in the interface and the implemention. The latter will have to wrapain anNSNumberusingnumberWithIntbecauseNSArraycan only storeNSObjectswhileintinstead is a base type.