[quote=“ibex10”][quote]NSLog(@"The top 3 are: %@", [portfolio topHoldings]);
get an error “No visible @interface for NSMutableArray declares the selector topHoldings”.[/quote]
What is the type of the portfolio variable?
I don’t have the 2nd Edition of the book, but looks like you are sending the topHoldings message to an instance of NSMutableArray. You need to send the message to an instance of the class (I am guessing it is the BNRPortfolio class) that declares the topHoldings method.[/quote]
I’m getting the “No known class” error message from the compiler. According to the code, I should be sending the NSLog to BNRPortfolio methods mostValuableHoldings and holdingsSortedBySymbol but it looks like it is being sent to an NSArray. Can someone help? I’ve been trying to get this to work for hours.
//
// BNRPortfolio.h
// Stocks
//
// Created by Nelson Capes on 7/31/15.
// Copyright © 2015 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
#import “BNRForeignStockHolding.h”
@class BNRStockHolding;
@interface BNRPortfolio : NSObject
{
NSMutableArray *_holdings;
}
-(void)setHolding: (NSMutableArray *)a;
-(void)addHolding:(NSMutableArray *)a;
-(NSArray *)mostValuableHoldings;
-(NSArray *)holdingsSortedBySymbol;
@end
//
// BNRPortfolio.m
// Stocks
//
// Created by Nelson Capes on 7/31/15.
// Copyright © 2015 Big Nerd Ranch. All rights reserved.
//
#import “BNRPortfolio.h”
#import “BNRStockHolding.h”
#import “BNRForeignStockHolding.h”
@implementation BNRPortfolio
-(void)addHolding: (NSMutableArray *)h
{
if(!_holdings){
_holdings = [[NSMutableArray alloc] init];
}
[_holdings addObject:h];
}
-(void)setHolding:(NSMutableArray *)h
{
_holdings = [h mutableCopy];
}
-(NSMutableArray *)holdings
{
return [_holdings copy];
}
-
(NSArray *)mostValuableHoldings
{
NSSortDescriptor *vid = [NSSortDescriptor sortDescriptorWithKey:@“valueInDollars” ascending:NO];
NSMutableArray *sortedArray = [[NSMutableArray alloc] init];
sortedArray = _holdings;
[sortedArray sortUsingDescriptors:@[vid]];
return @[sortedArray[0], sortedArray[1], sortedArray[2]];
}
-
(NSArray *)holdingsSortedBySymbol
{
NSSortDescriptor *s = [NSSortDescriptor sortDescriptorWithKey:@“symbol” ascending:YES];
NSMutableArray *sortedArray = [[NSMutableArray alloc] init];
sortedArray = _holdings;
[sortedArray sortUsingDescriptors:@[s]];
return sortedArray;
}
@end
//
// main.m
// Stocks
//
// Created by Nelson Capes on 7/28/15.
// Copyright © 2015 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
#import “BNRForeignStockHolding.h”
#import “BNRSymbol.h”
#import “BNRPortfolio.h”
@class BNRPortfolio;
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *holdings =[[NSMutableArray alloc]init];
BNRStockHolding *stock1 = [[BNRStockHolding alloc]init];
BNRStockHolding *stock2 = [[BNRStockHolding alloc] init];
BNRForeignStockHolding *stock3 = [[BNRForeignStockHolding alloc] init];
[holdings addObject:stock1];
[holdings addObject:stock2];
[holdings addObject:stock3];
stock1.purchaseSharePrice = 2.30;
stock1.currentSharePrice = 4.50;
stock1.numberOfShares = 40;
stock2.purchaseSharePrice = 2.30;
stock2.currentSharePrice = 4.50;
stock2.numberOfShares = 40;
stock3.purchaseSharePrice = 2.30;
stock3.currentSharePrice = 4.50;
stock3.numberOfShares = 40;
stock3.conversionRate = 0.94;
stock1.symbol = @"@XYZ";
stock2.symbol =@"@ABC";
stock3.symbol = @"@LMN";
NSLog(@"Top 3 most valuable holdings: %@", [BNRPortfolio mostValuableHoldings]);
NSLog(@"Holdings sorted by symbol: %@", [BNRPortfolio holdingsSortedBySymbol]);
}
return 0;