About the challenge of practice 22.4

removeHoldings () will come to an error like:
No visible @interface for ‘NSArray’ declares the selector ‘removeObjectAtIndex:’
anyone who can slove this will be nice for me.

here is me code:
DXJPortfolio.h

#import <Foundation/Foundation.h>
#import "DXJStockHolding.h"
//@class DXJStockHolding;
NS_ASSUME_NONNULL_BEGIN

@interface DXJPortfolio :NSObject

@property(nonatomic,copy)NSArray *holdings;

-(float)totalValue;
-(float)totalCost;
-(void)addHoldings:(DXJStockHolding *)dxj;
-(void)removeHlodings:(unsigned int)i;

-(void)addforeignHoldings:(DXJStockHolding *)fdxj;
@end

DXJPortfolio.m

#import "DXJPortfolio.h"
//#import "DXJStockHolding.h"
@interface DXJPortfolio ()
{
    NSMutableArray *_holidings;
}

@end

@implementation DXJPortfolio
    
-(void)setHoldings:(NSArray *)h{
    _holdings = [h mutableCopy];
}

-(NSArray *)holding{
    return [_holdings mutableCopy];
}

-(void)addHoldings:(DXJStockHolding *)dxj{
    
    if(!_holdings){
        _holdings  = [[NSMutableArray alloc]init];
    }
    [_holidings addObject:dxj];
}
-(void)addforeignHoldings:(DXJStockHolding *)fdxj{
    if(!_holidings){
        _holdings = [[NSMutableArray alloc]init];
        
    }
    [_holidings addObject:fdxj];
}
-(void)removeHlodings:(unsigned int)i{
    if(i < _holdings.count){
        [_holdings removeObjectAtIndex:i];
    }else{
        NSLog(@"error");
    }
}
- (float)totalValue
{
    // Sum up total value of the StockHoldings
    float sum = 0;
    for (DXJStockHolding *h in _holdings) {
        sum += [h valueInDollars];
    }
    return sum;
}

- (float)totalCost
{
    // Sum up total cost of the StockHoldings
    float sum = 0;
    for (DXJStockHolding *h in _holdings) {
        sum += [h costInDollars];
    }
    return sum;
}


@end

hi,I refer to your content but some error occurred,could you please slove my problem .About the challenge of practice 22.4

You have naming problems: _holdings or _holidings?

This will work:

[_holidings removeObjectAtIndex:i];

But this one won’t:

[_holdings removeObjectAtIndex:i];

Can you see why?

1 Like

Wow,you are right. its my fault ,thank you !