Today I found I needed a really simple stack class to solve an
Objective-C programming problem elegantly. Being a newbie, I headed
straight for the documentation, but couldn't find anything suitable.
Do I really have to go and implement my own stack class from scratch or
is it just called something else in the Foundation Kit?
thanks,
Andy
Michael Ash - 28 Jan 2006 17:53 GMT
> Today I found I needed a really simple stack class to solve an
> Objective-C programming problem elegantly. Being a newbie, I headed
> straight for the documentation, but couldn't find anything suitable.
>
> Do I really have to go and implement my own stack class from scratch or
> is it just called something else in the Foundation Kit?
Use an NSMutableArray. push = addObject:, peek = lastObject, pop =
removeLastObject. Be careful about memory management when popping, as the
object you get from lastObject could be destroyed when you call
removeLastObject, so you should retain and autorelease it first. For
maximum convenience, put these methods in a category so you don't have to
go through this manually every time you want to pop something.

Signature
Michael Ash
Rogue Amoeba Software
Peter Ammon - 29 Jan 2006 22:01 GMT
> Today I found I needed a really simple stack class to solve an
> Objective-C programming problem elegantly. Being a newbie, I headed
[quoted text clipped - 6 lines]
>
> Andy
Unlike some other platforms, Cocoa provides only three generic data
structures - array, set, and dictionary. But the implementations are
polymorphic, so an array will work fine as a stack.
-Peter

Signature
Pull out a splinter to reply.