Question

I'm stuck at a challenge of some book.

I created a NSArray property named "holdings" in h.file to be accessed, and NSMutableArray instance valuable named "_holdings" at the class extension in m.file. It's because other objects are not allowed to access the array.

When I compiled it , the error message came out, and said "....-[__NSArrayI addObject:]: unrecognized selector sent to instance /Users/Toshikiinami/Desktop/Screen Shot 2014-05-13 at 21.16.09.png0x10010b7b0..."enter image description here

I set the _holdings as NSMutableArray but this error is saying "_holdings is an NSArray instance", that is addObject: method doesn't work.

I appreciate any help :) Thank you!

BNRPortfolio.h

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

@interface BNRPortfolio : NSObject


@property(nonatomic, copy) NSArray *holdings;


- (float)totalValue;
- (void)addStock:(BNRStockHolding *)stockHolding;
- (void)removeStock:(unsigned int) i;




@end

BNRPortfolio.m

#import "BNRPortfolio.h"
#import "BNRStockHolding.h"

@interface BNRPortfolio()
{
    NSMutableArray *_holdings;

}
@end

@implementation BNRPortfolio

- (void)holdings:(NSArray *)a
{
    _holdings = [a mutableCopy];
}

- (NSArray *)setHoldings
{

    return [_holdings copy];

}

- (float)totalValue
{
    float total = 0;

    for (int i = 0; i < 3; i++)
    {
        float currentValue = [_holdings[i] valueInDollars];
        total += currentValue;
    }

    return total;
}

- (void)addStock:(BNRStockHolding *)stockHolding
{
    if (!_holdings)
    {
        _holdings = [[NSMutableArray alloc] init];
    }

    [_holdings addObject:stockHolding];


}

- (void)removeStock:(unsigned int)i
{
    if (_holdings)
    {
        [_holdings removeObjectAtIndex:i];

    }

}

@end

main.m

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"
#import "BNRPortfolio.h"


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

    @autoreleasepool {

        BNRStockHolding *stock1 = [[BNRStockHolding alloc]init];
        BNRStockHolding *stock2 = [[BNRStockHolding alloc] init];
        BNRForeignStockHolding *stock3 = [[BNRForeignStockHolding alloc] init];

        stock1.purchaseSharePrice = 2.30;
        stock1.currentSharePrice = 4.50;
        stock1.numberOfShares = 40;
        stock1.symbol = @"XYZ";

        stock2.purchaseSharePrice = 12.19;
        stock2.currentSharePrice = 10.56;
        stock2.numberOfShares = 90;
        stock2.symbol = @"ABC";

        stock3.purchaseSharePrice = 45.10;
        stock3.currentSharePrice = 49.51;
        stock3.numberOfShares = 210;
        stock3.conversionRate = 0.94;
        stock3.symbol = @"LMN";


        NSMutableArray *mutable = [NSMutableArray array];
        [mutable addObject:stock1];
        [mutable addObject:stock2];
        [mutable addObject:stock3];

        BNRPortfolio *portfolio = [[BNRPortfolio alloc] init];
        portfolio.holdings = [mutable copy];

        float totalAmount = [portfolio totalValue];

        NSLog(@"total amount is %f",totalAmount);


//        for (BNRStockHolding *n in mutable)
//        {
//            NSLog(@"%f",[n costInDollars]);
//            NSLog(@"%f", [n valueInDollars]);
//        }


/*

        for (int i = 0; i < [mutable count]; i++ )
        {
            NSLog(@" %d 's costInDollars = %.1f",i,[mutable[i] costInDollars]);
            NSLog(@" %d 's valueInDollars = %.1f",i,[mutable[i] valueInDollars]);

        }

 */
       BNRStockHolding *stock4 = [[BNRStockHolding alloc] init];

        stock4.purchaseSharePrice = 42.10;
        stock4.currentSharePrice = 22.51;
        stock4.numberOfShares = 230;
        stock4.symbol = @"LMN";


        [portfolio addStock:stock4];

        NSLog(@"count portfoio %ld",[portfolio.holdings count]);

    }
    return 0;
}
Was it helpful?

Solution

I don't directly see why your code isn't working, as there is no [NSArray alloc] in the code you posted, so it must be somewhere else.

- (void)holdings:(NSArray *)a
{
    _holdings = [a mutableCopy];
}

- (NSArray *)setHoldings
{

    return [_holdings copy];

}

these really need to change to

- (void)setHoldings:(NSArray *)a
{
    _holdings = [a mutableCopy];
}

- (NSArray *)holdings
{
    return [_holdings copy];
}

I have no idea what would even happen with the code portfolio.holdings = [mutable copy];

It would call [portfolio setHoldings:[mutable copy]], but you currently have no method called setHoldings that takes a parameter. Because of the dot syntax, I suspect it just calls [portfolio setHoldings] and sets the copy of the _holdings array to the value [mutable copy] which would then do nothing because of the return of the array being a copy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top