由于Object-C中没有提供Stack容器,因此自己实践了一个简单的stack容器 #import <Foundation/Foundation.h> @interface NSStack : NSObject { NSMutableArray* m_array; int count; } - (void)push:(id)anObject; - (id)pop; - (void)clear; @property (nonatomic, readonly) int count; @end
#import "NSStack.h" @implementation NSStack @synthesize count; - (id)init { if( self=[super init] ) { m_array = [[NSMutableArray alloc] init]; count = 0; } return self; } - (void)dealloc { [m_array release]; [self dealloc]; [super dealloc]; } - (void)push:(id)anObject { [m_array addObject:anObject]; count = m_array.count; } - (id)pop { id obj = nil; if(m_array.count > 0) { obj = [[[m_array lastObject]retain]autorelease]; [m_array removeLastObject]; count = m_array.count; } return obj; } - (void)clear { [m_array removeAllObjects]; count = 0; } @end