Quote:
Originally Posted by SamRhoads
I want to create a class called Stack. I want to be able to add methods like push, pop, and peek. I think such a class would have an NSMutableArray inside, but I don't see why it needs to be a subclass of NSMutableArray. I want to have some additonal special purpose methods in the class.
|
NSMutableArray has an "addObject:" method, which is equivalent to push, and "removeLastObject:" which you could combine with the "lastObject:" method it inherits from NSArray.
You could subclass from NSMutableArray and just add the methods you need; the only reason not to subclass would be to restrict access to the default methods of NSArray and NSMutableArray. But then you'll have to write your own copy methods, etc.
You could also use a "category" to add your custom methods to NSMutableArray without creating a new class, but that might be a bit confusing if you haven't studied it before. Categories let you add methods (but not variables) to existing classes, even system classes.
Quote:
and I want to be able to assign a current stack to a new stack:
Stack newStack = stackA;
|
That'll give you two pointers to the same stack. You'll want to use [stackA copy] instead if you need a separate stack.