Reputation: 31
I've checked Google and I haven't found anything that helps. I'm writing a stack class in Objective-C, the stack is based around an NSMutableArray, however, I cannot add any objects to it, it's not throwing any errors in the console, and there are no compile warnings/errors. Here is my code for the stack object.
#import "Stack.h"
@implementation Stack
@synthesize stack;
- (id)init {
self.stack = [[NSMutableArray alloc] init];
return self;
}
- (void)push:(id)object { [self.stack addObject:object]; }
- (int)size { return [self.stack count]; }
- (id)pop {
id obj = [[[self.stack lastObject] retain] autorelease];
[self.stack removeLastObject];
return obj;
}
- (id)peek { return [self.stack lastObject]; }
@end
Header:
#import <Cocoa/Cocoa.h>
@interface Stack : NSObject {
NSMutableArray *stack;
}
- (void)push:(id)object;
- (int)size;
- (id)pop;
- (id)peek;
@property (nonatomic, retain) NSMutableArray *stack;
@end
For the rest of the code, if I call [test_stack size], it returns zero, no matter how many times I push an object, and if I call pop or peek, it simply returns (null).
#import "TRIAL_Stack_Ctrl.h"
@implementation TRIAL_Stack_Ctrl
@synthesize test;
- (IBAction)push:(id)sender {
[test_stack push:[input stringValue]];
}
- (IBAction)pop:(id)sender {
[label setStringValue:[NSString stringWithFormat:@"%@", [test_stack pop]]];
}
- (IBAction)peek:(id)sender {
[label setStringValue:[NSString stringWithFormat:@"%@", [test_stack peek]]];
}
- (IBAction)size:(id)sender {
[label setStringValue:[NSString stringWithFormat:@"%d", [test_stack size]]];
}
@end
This leads me to believe that it's not pushing the object, is there anything I am doing wrong?
Upvotes: 0
Views: 2989
Reputation: 86651
This leads me to believe that it's not pushing the object, is there anything I am doing wrong?
Incorrect assumption. removeLastObject throws an NSRangeException if the mutable array has no objects in it. If you do not see a range exception, when you try to pop an empty stack, the stack itself must be nil.
Upvotes: 1
Reputation: 9198
Apart from leaking the NSMutableArray, and unnecessary use of self.stack
, it looks ok. So it seems your problem is probably in TRIAL_Stack_Ctrl class that you don not show the code to.
If you feel like you are going crazy assertions can help you get to the bottom of what is going on.
- (void)push:(id)object {
NSParameterAssert(object);
[stack addObject:object];
NSAssert([stack count], @"array is empty");
}
They compile away to nothing in release code.
Upvotes: 1
Reputation: 237010
If that really is the full implementation of your TRIAL_Stack_Ctrl
class, you're not assigning the test stack instance variable anywhere, so it's nil.
Upvotes: 1
Reputation: 4428
Can we see the .h for the implementation? I see you are synthesizing something called 'test' but all the operations are done on something called 'test_stack'. Naming problem? If so, it should probably also be 'self.test_stack'.
Upvotes: 0
Reputation: 15927
Change:
- (id)init {
self.stack = [[NSMutableArray alloc] init];
return self;
}
to:
- (id)init {
self = [super init];
if (self) {
stack = [[NSMutableArray alloc] init];
}
return self;
}
Upvotes: 2