Search code examples
objective-cfor-loopnsmutablearrayfast-enumeration

Fast Enumeration on Array with similar Objects


I am quite confused on doing fast enumeration with array filled with similar objects

Suppose :

I have 1 class (Stock Class), it has 1 subClass ForeignStock.

Properties of Stock Class :

@property float purchaseSharePrice,currentSharePrice;
@property int numberOfShares;

Properties of ForeignStock

@property float conversionRate;

I put instances from those 2 above into a MutableArray. How do I display(NSLog) those 2 different Objects with Fast Enumeration?

 int i = 1;
    for (StockHolding *iterate in shelf) {

        if ([iterate isMemberOfClass:[StockHolding class]])
        {

            NSLog(@"============ Properties of Stock Value %i ============",i);
            NSLog(@"purchase price is  %f",iterate.purchaseSharePrice);
            NSLog(@"current price is %f",iterate.currentSharePrice);
            NSLog(@"number of shares bought is %i",iterate.numberOfShares);

            NSLog(@"--------------Total Value & Cost--------------");
            NSLog(@"Value in dollar for this stock is %f",iterate.valueInDollars);
            NSLog(@"Cost in dollar for this stock is %f",iterate.costInDollars);


            i++;
        }
        else{
            for (ForeignStockHolding *iterate1 in shelf) {
                if ([iterate1 isMemberOfClass:[ForeignStockHolding class]]) {
                    NSLog(@"============ Properties of Stock Value %i  ============",i);
                    NSLog(@"purchase price is  %f",iterate1.purchaseSharePrice);
                    NSLog(@"current price is %f",iterate1.currentSharePrice);
                    NSLog(@"number of shares bought is %i",iterate1.numberOfShares);

                    NSLog(@"--------------Total Value, Cost, & Conversion Rate --------------");
                    NSLog(@"Value in dollar for this stock is %f",iterate1.valueInDollars);
                    NSLog(@"Cost in dollar for this stock is %f",iterate1.costInDollars);
                    NSLog(@"Conversion rate for this stock is %f",iterate1.conversionRate);

                    i++;
                }
            }
        }
    }

Those code above didn't work out, the output for ForeignStock NSLogged 2 times for each ForeignStock instance (I know, for method at second fast enumeration is wrong).

How do I build fast enumeration which it could differ each object's class inside array with different treatment for each class-subclass objects?


Solution

  • If the problem you're trying to solve it to get a good representation of an object for debugging, then consider overriding the -description method in your class. Here's an example of that for both these classes:

    @implementation StockHolding
    
    - (NSString *)description
    {
        NSString *objectDescriptionFormat = 
        @"============ Properties of Stock Value %@ ============\n"
        "purchase price is  %f\n"
        "current price is %f\n"
        "number of shares bought is %i\n"
        "\n"
        "--------------Total Value & Cost--------------\n"
        "Value in dollar for this stock is %f\n"
        "Cost in dollar for this stock is %f\n";
    
        return [NSString stringWithFormat:objectDescriptionFormat, 
            [super description],
            self.purchaseSharePrice,
            self.currentSharePrice,
            self.numberOfShares,
            self.valueInDollars,
            self.costInDollars];
    }
    
    @end
    
    
    @implementation ForeignStockHolding
    
    - (NSString *)description
    {
        NSString *objectDescriptionFormat = 
        @"%@"
        "Conversion rate for this stock is %f\n";
    
        return [NSString stringWithFormat:objectDescriptionFormat, 
            [super description],
            self.conversionRate];
    }
    
    @end
    

    Logging the object becomes easy because you can just log the object and the description will get printed on the console.

    for (StockHolding *stockHolding in shelf)
    {
        NSLog(@"%@", stockHolding);
    }