A strange thing about the challenge of Ch. 22

Hi everyone,

I am new to objective-C programming and doing the challenge of Ch. 22 now. I found something strange.

I override the dealloc method of BNRStockHolding.m, to show a message when a BNRStockHolding is removed from the portfolio. I use the codes from the BNREmployee example in the book.

Here are the codes:

maim.m

[code]#import <Foundation/Foundation.h>
#import “BNRForeignStockHolding.h”
#import “BNRPortfolio.h”

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

@autoreleasepool {
    
    // Create 3 instances of BNRStockHolding
    BNRStockHolding *stockHolding1 = [[BNRStockHolding alloc] init];
    stockHolding1.symbol = @"XYZ";
    stockHolding1.purchaseSharePrice = 2.30;
    stockHolding1.currentSharePrice = 4.50;
    stockHolding1.numberOfShares = 40;
    
    BNRStockHolding *stockHolding2 = [[BNRStockHolding alloc] init];
    stockHolding2.symbol = @"ABC";
    stockHolding2.purchaseSharePrice = 2.30;
    stockHolding2.currentSharePrice = 4.50;
    stockHolding2.numberOfShares = 40;
    
    BNRForeignStockHolding *stockHolding3 = [[BNRForeignStockHolding alloc] init];
    stockHolding3.symbol = @"LMN";
    stockHolding3.purchaseSharePrice = 2.30;
    stockHolding3.currentSharePrice = 4.50;
    stockHolding3.numberOfShares = 40;
    stockHolding3.conversionRate = 0.94;
    
    
    // Create a BNRPortfolio
    BNRPortfolio *portfolio = [[BNRPortfolio alloc] init];
    
    // Add stockholdings to portfolio
    [portfolio addHolding:stockHolding1];
    [portfolio addHolding:stockHolding2];
    [portfolio addHolding:stockHolding3];
    
    
    // Interate this array and print results
    for (BNRStockHolding *s in portfolio.holdings) {
        NSLog(@"<Holding %@> cost: %.2f, value: %.2f.\n", s, [s costInDollars], [s valueInDollars]);
    }
    
    NSLog(@"Total value is %.2f.\n", [portfolio totalValue]);
    
    
    // Remove a particular holding but its symbol, say, ABC
    NSLog(@"Remove holding ABC");
    NSString *holdingToRemove = @"ABC";
    for (BNRStockHolding *h in portfolio.holdings) {
        if ([h.symbol isEqualToString:holdingToRemove]) {
            [portfolio removeHolding:h];
            break;
        }
    }
    
}
return 0;

}
[/code]

BNRPortfolio.h (add/removeHolding method part)

[code]- (void)addHolding:(BNRStockHolding *)h;

  • (void)removeHolding:(BNRStockHolding *)h;[/code]

BNRPortfolio.m (add/removeHolding method part)

[code]// Method to add/remove a holding

  • (void)addHolding:(BNRStockHolding *)h
    {
    // Is it nil?
    if (!_holdings) {
    _holdings = [[NSMutableArray alloc] init];
    }
    [_holdings addObject:h];
    }

  • (void)removeHolding:(BNRStockHolding *)h
    {
    if (_holdings) {
    [_holdings removeObject:h];
    }
    }
    [/code]

BNRStockHolding.h

[code]#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

// BNRStockHolding have 3 instance variable

@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;
@property (nonatomic) NSString *symbol;

// Other methods

  • (float)costInDollars;
  • (float)valueInDollars;

@end[/code]

BNRStockHolding.m

[code]#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float)costInDollars
    {
    return self.purchaseSharePrice * self.numberOfShares;
    }

  • (float)valueInDollars
    {
    return self.currentSharePrice * self.numberOfShares;
    }

  • (NSString *)description
    {
    return _symbol;
    }

// Override the dealloc method

  • (void)dealloc
    {
    NSLog(@“deallocating %@”, self);
    }

@end[/code]

Run this program it returns:

2014-08-24 22:19:53.793 Stockes[3655:303] <Holding XYZ> cost: 92.00, value: 180.00. 2014-08-24 22:19:53.794 Stockes[3655:303] <Holding ABC> cost: 92.00, value: 180.00. 2014-08-24 22:19:53.795 Stockes[3655:303] <Holding LMN> cost: 86.48, value: 169.20. 2014-08-24 22:19:53.795 Stockes[3655:303] Total value is 529.20. 2014-08-24 22:19:53.795 Stockes[3655:303] Remove holding ABC 2014-08-24 22:19:53.796 Stockes[3655:303] deallocating XYZ 2014-08-24 22:19:53.796 Stockes[3655:303] deallocating ABC 2014-08-24 22:19:53.796 Stockes[3655:303] deallocating LMN

It seems all three BNRStockHolding objects are removed. I noticed that the only portfolio object has no owner, it will be removed in the end and so will other BNRStockHolding objects owned by it.
So how to remove just the holding I want to and keep the others?