Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 10-07-2009, 06:49 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 10
Default Creating a Stack class

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. Once created, I want to be able to create several stacks:

Stack stackA;
Stack stackB;

and so on. I then need to be able to push and pop to and from these stacks:

Something *item = [stackA pop];

and I want to be able to assign a current stack to a new stack:

Stack newStack = stackA;

I guess it's clear that I come from a Java background, but I still want to do this. Is there a way to do it? Everything I try crashes.

Thanks,

Sam.
SamRhoads is offline   Reply With Quote
Old 10-07-2009, 07:27 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by SamRhoads View Post
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.
__________________

Free Games!

Last edited by smasher; 10-07-2009 at 07:29 PM.
smasher is offline   Reply With Quote
Old 10-07-2009, 08:54 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 10
Default

Sure, I know that.

Quote:
Originally Posted by smasher View Post
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.

That'll give you two pointers to the same stack. You'll want to use [stackA copy] instead if you need a separate stack.
I did not try using categories. Maybe that would have done what I wanted.

And I do want two references to the same stack.

Anyway, I finally did create a new class that is NOT a subclass of NSMutableArray, and it behaves JUST like a Stack should. I'll post the code if anyone is interested.

Sam.
SamRhoads is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone app, nsmutablearray, objective-c, stack

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 265
14 members and 251 guests
2WeeksToGo, AdamL, ADY, BrianSlick, Dani77, Dattee, headkaze, mer10, prchn4christ, smithdale87, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,747
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:50 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0