Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Stack class for Cocoa?

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  Problem with LaunchApplication! Make it synchrono..  
Author Message
Lorenzo Thurman

External


Since: May 23, 2004
Posts: 93



(Msg. 1) Posted: Thu May 31, 2007 10:25 am
Post subject: Stack class for Cocoa?
Archived from groups: comp>sys>mac>programmer>help (more info?)

Is there no stack class or equiv in Cocoa? Do I have to roll my own?
I'll probably just use an array, unless there are better suggestions.
TIA
Back to top
Login to vote
Michael Ash

External


Since: Jan 18, 2005
Posts: 1073



(Msg. 2) Posted: Thu May 31, 2007 12:41 pm
Post subject: Re: Stack class for Cocoa? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Lorenzo Thurman wrote:
> Is there no stack class or equiv in Cocoa? Do I have to roll my own?
> I'll probably just use an array, unless there are better suggestions.

NSMutableArray will do the job just fine. Push is addObject:, pop is a
lastObject followed by a removeLastObject. (Remember to either finish
using the object or retain it before the remove.) There's no point in
having a separate stack class when an existing one will do. You can do
similar things to use them as a queue.

If you find yourself doing this a lot, adding push/pop methods in a
category could be useful.

--
Michael Ash
Rogue Amoeba Software
Back to top
Login to vote
Lorenzo Thurman

External


Since: May 23, 2004
Posts: 93



(Msg. 3) Posted: Thu May 31, 2007 1:47 pm
Post subject: Re: Stack class for Cocoa? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Michael Ash wrote:
> Lorenzo Thurman wrote:
>> Is there no stack class or equiv in Cocoa? Do I have to roll my own?
>> I'll probably just use an array, unless there are better suggestions.
>
> NSMutableArray will do the job just fine. Push is addObject:, pop is a
> lastObject followed by a removeLastObject. (Remember to either finish
> using the object or retain it before the remove.) There's no point in
> having a separate stack class when an existing one will do. You can do
> similar things to use them as a queue.
>
> If you find yourself doing this a lot, adding push/pop methods in a
> category could be useful.
>
A mutable one of course. I'd already done the methods, they were trivial
enough. What I hadn't thought about was just adding the methods via a
category.
Questions are powerful tools.
Thanks for the reply.
Back to top
Login to vote
Gregory Weston

External


Since: Mar 26, 2005
Posts: 3646



(Msg. 4) Posted: Thu May 31, 2007 1:59 pm
Post subject: Re: Stack class for Cocoa? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article ,
Lorenzo Thurman wrote:

> Is there no stack class or equiv in Cocoa? Do I have to roll my own?
> I'll probably just use an array, unless there are better suggestions.
> TIA

Use a mutable array. It doesn't enforce stack-like behavior, but it's
trivial to add things to the end, retrieve the last item and remove the
last item. You could even make it a category.

-------------8<---------------------------

#warning Typed into news reader.

@interface NSMutableArray (GWStack)
- (void)push:(id)inObject;
- (id)pop;
@end

@implementation NSMutableArray (GWStack)

- (void)push:(id)inObject
{
if(inObject) [self addObject:inObject];
}

- (id)pop
{
id theResult = nil;
if([self count])
{
theResult = [[[self lastObject] retain] autorelease];
[self removeLastObject];
}
return theResult;
}

@end
Back to top
Login to vote
Lorenzo Thurman

External


Since: May 23, 2004
Posts: 93



(Msg. 5) Posted: Thu May 31, 2007 1:59 pm
Post subject: Re: Stack class for Cocoa? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Gregory Weston wrote:
> In article ,
> Lorenzo Thurman wrote:
>
>> Is there no stack class or equiv in Cocoa? Do I have to roll my own?
>> I'll probably just use an array, unless there are better suggestions.
>> TIA
>
> Use a mutable array. It doesn't enforce stack-like behavior, but it's
> trivial to add things to the end, retrieve the last item and remove the
> last item. You could even make it a category.
>
> -------------8<---------------------------
>
> #warning Typed into news reader.
>
> @interface NSMutableArray (GWStack)
> - (void)push:(id)inObject;
> - (id)pop;
> @end
>
> @implementation NSMutableArray (GWStack)
>
> - (void)push:(id)inObject
> {
> if(inObject) [self addObject:inObject];
> }
>
> - (id)pop
> {
> id theResult = nil;
> if([self count])
> {
> theResult = [[[self lastObject] retain] autorelease];
> [self removeLastObject];
> }
> return theResult;
> }
>
> @end
Thanks for the reply. I'm going to add this stuff as a category on the
NSMutableArray class.
Back to top
Login to vote
David C.

External


Since: Oct 11, 2003
Posts: 1609



(Msg. 6) Posted: Sat Jun 02, 2007 7:21 pm
Post subject: Re: Stack class for Cocoa? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Michael Ash writes:
> Lorenzo Thurman wrote:
>> Is there no stack class or equiv in Cocoa? Do I have to roll my own?
>> I'll probably just use an array, unless there are better suggestions.
>
> NSMutableArray will do the job just fine. Push is addObject:, pop is a
> lastObject followed by a removeLastObject. (Remember to either finish
> using the object or retain it before the remove.) There's no point in
> having a separate stack class when an existing one will do. You can do
> similar things to use them as a queue.

Or, if you're using C++ (or Objective-C++), you could use an appropriate
STL container template.

-- David
Back to top
Login to vote
Clark Cox

External


Since: May 21, 2007
Posts: 4



(Msg. 7) Posted: Sat Jun 02, 2007 7:21 pm
Post subject: Re: Stack class for Cocoa? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2007-06-02 12:21:43 -0700, shamino RemoveThis @techie.com (David C.) said:

> Michael Ash writes:
>> Lorenzo Thurman wrote:
>>> Is there no stack class or equiv in Cocoa? Do I have to roll my own?
>>> I'll probably just use an array, unless there are better suggestions.
>>
>> NSMutableArray will do the job just fine. Push is addObject:, pop is a
>> lastObject followed by a removeLastObject. (Remember to either finish
>> using the object or retain it before the remove.) There's no point in
>> having a separate stack class when an existing one will do. You can do
>> similar things to use them as a queue.
>
> Or, if you're using C++ (or Objective-C++), you could use an appropriate
> STL container template.

However, you'd still have to add the appropriate -retain/-release calls
for objects stored in the container. In which case, you're better off
just using NSMutableArray.


--
Clark S. Cox III
Back to top
Login to vote
Display posts from previous:   
Related Topics:
stack trace - Is there any way to dump a stack trace from a running CFM application? I would like to put in some code to dump it at ...

Stack and "as" assembler in Xcode - This is code assembled by "as" and disassembled by Xcode 0x0001b618 <+0000> mflr r0 0x0001b61...

capturing crash reporter stack trace? - I am running some native CFM code in a bundle as a native library for Java JNI invocation. This requires some tricks, ...

Which class do I need ? - As a newbie mac programmer (but longtime C/Posix programmer) I've been teaching myself Cocoa programming on Mac OS X v...

unloading a class - Once I have my initialization class loaded and executed, i no longer need to have it loaded into memory. is there a wa...

help on extending a class - I'd like to extend NSMutableArray with this @interface NSMutableArray (CSVs) - (id)initWithContentsOfCSV:(NSString *...
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA



[ Contact us | Terms of Service/Privacy Policy ]