My Solution for holdings 1 & 2


#1

BNRPortfolio.m

[code]

  • (NSArray *) topThreeHoldings
    {

    NSMutableArray *topThreeStocks = [[NSMutableArray alloc] init];
    topThreeStocks = [self.holdings mutableCopy];

    NSSortDescriptor *sortedByValueInDollars = [NSSortDescriptor sortDescriptorWithKey:@“valueInDollars” ascending:NO];
    [topThreeStocks sortUsingDescriptors:@[sortedByValueInDollars]];

    if ([topThreeStocks count] > 3) {
    for (NSUInteger i = [topThreeStocks count]; i > 3; i–) {
    [topThreeStocks removeLastObject];
    }
    }

    return topThreeStocks;
    }

  • (NSArray *) allStocksSorted
    {
    NSMutableArray *sortStocksByTicker = [[NSMutableArray alloc]init];
    sortStocksByTicker = [self.holdings mutableCopy];

    NSSortDescriptor *sortedAlphabetically = [NSSortDescriptor sortDescriptorWithKey:@"symbol"
    ascending:YES];
    [sortStocksByTicker sortUsingDescriptors:@[sortedAlphabetically]];
    return sortStocksByTicker;
    }[/code]

main.m

[code]
NSArray *topThreeValuableStocks = [NSArray arrayWithArray:[stockPortfolio topThreeHoldings]];
NSArray *allStocksSortedBySymbol = [NSArray arrayWithArray:[stockPortfolio allStocksSorted]];

    NSLog(@"Top three most valuable holdings are: %@", topThreeValuableStocks);
    NSLog(@"All stocks sorted alphabetically by symbol: %@", allStocksSortedBySymbol);[/code]

BNRStockHolding.m

[code]

  • (NSString *)description
    {

    return [NSString stringWithFormat:@“Ticker Symbol: %@, Profit: %.2f, ValueInDollars: %.2f”,
    self.symbol, [self gainOrLoss], [self valueInDollars]];
    }[/code]


#2

This is my solution, hope it helps someone

BNRPortfolio.m

[code]- (NSArray *)topThreeHoldings
{
if(_holdings)
{
// New mutabel array
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:self.holdings];

    // Sort array
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"valueInDollars" ascending:NO];
    [mutableArray sortUsingDescriptors:@[sortDescriptor]];
    
    // Return top three values
    return [mutableArray subarrayWithRange:NSMakeRange(0, 3)];
}

// return pass
return 0;

}[/code]


#3

You don’t need the following lines:

  [quote] NSArray *topThreeValuableStocks = [NSArray arrayWithArray:[stockPortfolio topThreeHoldings]];
    NSArray *allStocksSortedBySymbol = [NSArray arrayWithArray:[stockPortfolio allStocksSorted]];[/quote]

You already created the arrays in your instance method in BNRPortolio. Just use your 2 lines of NSLog to send the message to BNRPortfolio array (the following assumes you named it portfolio)

[quote]NSLog(@“Top three most valuable holdings are: %@”, portfolio.topThreeHoldings);
NSLog(@“All stocks sorted alphabetically by symbol: %@”, portfolio.allStocksSorted);[/quote]


#4

[quote=“OkayGreat”]BNRPortfolio.m

    
    if ([topThreeStocks count] > 3) {
        for (NSUInteger i = [topThreeStocks count]; i > 3; i--) {
            [topThreeStocks removeLastObject];
        }
    }
 

[/quote]

You actually don’t even need the if statement. It works fine just removing it, but it is a nice check to skip the for loop if the number of holdings you have is already 3 or less.
I’m of the opinion though that to ensure a bug-free line of code (and you are able to), just do away with the line of code completely if its not needed. It makes the code less complicated to read.
So if it was me, I would ditch the if statement, but maybe I’m just lazy.


#5

[quote=“rabyjrm”]You don’t need the following lines:

  [quote] NSArray *topThreeValuableStocks = [NSArray arrayWithArray:[stockPortfolio topThreeHoldings]];
    NSArray *allStocksSortedBySymbol = [NSArray arrayWithArray:[stockPortfolio allStocksSorted]];[/quote]

You already created the arrays in your instance method in BNRPortolio. Just use your 2 lines of NSLog to send the message to BNRPortfolio array (the following assumes you named it portfolio)

[quote]NSLog(@“Top three most valuable holdings are: %@”, portfolio.topThreeHoldings);
NSLog(@“All stocks sorted alphabetically by symbol: %@”, portfolio.allStocksSorted);[/quote][/quote]

I think this only works if you override the description method? If you dont, you just get output like this (at least when I tried this I did):

2014-09-12 12:32:07.039 Challenge_Attempt2_Stocks[11714:303] ( "<BNRStockHolding: 0x1001085f0>", "<BNRForeignStockHolding: 0x10010ac30>", "<BNRStockHolding: 0x100103340>" )