addObject Question


#1

Hi,

I had a specific question related to how one might implement a method to pass multiple arguments to NSMutableArray. For example, my code in Portfolio.m to implement a method for the addStockObject to a stockPortfolio array looks something like the following:

- (void)addStockObject:(StockHolding *)a
{
    // Is portfolio nil?
    if (!stockPortfolio) {
        
        // Create the array
        stockPortfolio = [[NSMutableArray alloc] init];
    }
    [stockPortfolio addObject:a];
}

I know that it’s been discussed earlier in the text that you can create arrays using something like:

NSMutableArray  *somePointerVariable = [NSMutableArray arrayWithObjects:firstObject, secondObject, thirdObject, ..., nil ];

How does one go about implementing this behavior in a class so that I might be able to add multiple objects to the stockPortfolio array in one line. For example, I currently add a stock object by:

[stockPortfolio addStockObject:AAPL];

and I would like to know how I might add the ability to do this:

[stockPortfolio addStockObjects:AAPL, GOOG, ..., nil ];

Any instruction would be appreciated. Thanks!