Top holding challenge help


#1

I keep getting an error that I do not fully understand. I was wondering if someone can help pint out the error. This is the error that shows up:

__NSArrayI sortUsingDescriptors:]: unrecognized selector sent to instance 0x1001111e0
2014-11-11 21:39:30.257 MRStock[3691:32115] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSArrayI sortUsingDescriptors:]: unrecognized selector sent to instance 0x1001111e0’

Main.m


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


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

        //set values
        MRStock *stock1 = [[MRStock alloc] init];
        stock1.purchasePrice = 2.3;
        stock1.currentPrice = 4.5;
        stock1.numberOfShares = 40;
        stock1.Ticker = @"APPL";
                
        MRStock *stock2 = [[MRStock alloc] init];
        stock2.purchasePrice = 12.19;
        stock2.currentPrice = 10.56;
        stock2.numberOfShares = 90;
        stock2.Ticker = @"CSCO";
        
        
         MRForeignstock *stock3 = [[MRForeignstock alloc] init];
        stock3.purchasePrice = 45.10;
        stock3.currentPrice = 49.51;
        stock3.numberOfShares = 210;
        stock3.conversionRate = 0.94;
        stock3.Ticker = @"AZ";
        
        
        // load up the array
        NSMutableArray *stocks = [[NSMutableArray alloc] initWithObjects:stock1, stock2, stock3, nil];
        
        for (MRStock *s in stocks){
            
            NSLog(@"%.2f", s.costInDollars);
            NSLog(@"%.2f", s.valueInDollars);
            
        }
        
        
        
        MRPortfolio *holdings = [[MRPortfolio alloc] init];
        
        holdings.holdings = [[NSMutableArray alloc] initWithObjects:stock1, stock2, stock3, nil];
       
    
        NSLog(@"your portfolio is worth %.2f", holdings.totalValue);
        
        
        NSArray *topThree = holdings.topHoldings;
       
        
        for (MRForeignstock *voh in holdings.topHoldings){
            
            NSLog(@"%@", voh);
            
        }
        
    }
    return 0;
}

MRPortfolio.h



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

@interface MRPortfolio : NSObject

@property (nonatomic, copy) NSArray *holdings;

- (float) totalValue;
- (NSArray *)topHoldings;


@end

MRPortfolio.m



#import "MRPortfolio.h"

@interface MRPortfolio ()


{
    NSMutableArray *_holdings;
    
}

- (void) addStock:(MRForeignstock *)a;
- (void) removeStock:(MRForeignstock *)b;


@end


@implementation MRPortfolio


- (float) totalValue {
    
    float sum = 0;
    
    for (MRForeignstock *b in _holdings){
        
        sum += [b valueInDollars];
    }
    return sum;
    
}

- (void)addStock:(MRForeignstock *)a{
    if (!_holdings) {
        _holdings = [[NSMutableArray alloc] init];
    }
    
    [ _holdings addObject:a];
    
}

- (void) removeStock:(MRForeignstock *)b{

    
    [_holdings removeObject:b];
    
}

- (NSArray *)topHoldings{
    
    NSSortDescriptor *vid = [NSSortDescriptor sortDescriptorWithKey:@"valueInDollars"
                                                            ascending:NO];
    [_holdings sortUsingDescriptors:@[vid]];
    
    NSArray *topStocks = [[NSArray alloc] initWithArray:_holdings];
    
    return topStocks;
    
}
@end

#2

[quote][code]
@implementation MRPortfolio

  • (NSArray *)topHoldings {

    NSSortDescriptor *vid = [NSSortDescriptor sortDescriptorWithKey:@“valueInDollars” ascending:NO];

    [_holdings sortUsingDescriptors:@[vid]];

    NSArray *topStocks = [[NSArray alloc] initWithArray:_holdings];

    return topStocks;
    }
    @end
    [/code][/quote]

[quote]‘NSInvalidArgumentException’, reason: ‘-[__NSArrayI [color=#FF0000]sortUsingDescriptors:[/color]]: unrecognized selector sent to instance 0x1001111e0’
[/quote]
That message is saying that NSArray does not have a method named sortUsingDescriptors:.

Did you check to see if the method sortUsingDescriptors: does exist in NSArray’s interface?

Maybe, you meant to use the sortedArrayUsingDescriptors: method:

@implementation MRPortfolio
...
- (NSArray *)topHoldings {
    
    NSSortDescriptor *vid = [NSSortDescriptor sortDescriptorWithKey:@"valueInDollars" ascending:NO];

    NSArray *topStocks = [_holdings sortedArrayUsingDescriptors:@[vid]];

    return topStocks;
}
@end

#3

Hi, Thanks for the tip. I tried it out and it gave me the following errors :

  • Implicit conversion of an Objective-C pointer to ‘SEL’ is disallowed with ARC
  • Incompatible pointer types sending ‘NSArray *’ to parameter of type ‘SEL’

#4

Disable the ARC then.