Portfolio as sub-class of NSMutableArray


#1

Looking at the solutions on here, most create the “Portfolio” as a sub-class of NSObject.

As the key function of the portfolio is to store an array of share holdings (with the added ability to return the sum of itself), why do people not make the “Portfolio” class a sub-class of NSMutableArray?

Thanks!


#2

Portfolio class is not really a container class; it just happens to use the NSMutableArray class to create a storage medium. Therefore it is more desirable for Portfolio to inherit from a more abstract class such as NSObject and use composition to implement its functionality.

NSMutable array can certainly be subclassed, but I suspect doing so will involve more work. You should try it as an exercise.


#3

Thanks. I briefly tried that approach but was getting a run time error when trying to “addObject” to the array.