I just started coding in Objective-C and would like to know if my simple implementation of a Stack is acceptable & what ways would you improve the Stack code, or the Main code? i'm curious of things such as (but not limited to):
- formatting
- edge cases
- run time (like one part where try to manipulate strings in
description
) - general good practice
- memory management practices (i'm using ARC in this code)
Stack.h:
#import <Foundation/Foundation.h>
@interface Stack : NSObject
-(void)push:(id)obj;
-(id)pop;
-(NSUInteger)size;
-(id)peek;
-(BOOL)isEmpty;
@end
Stack.m file:
#import "Stack.h"
@implementation Stack{
NSMutableArray *stack;
}
-(id)init{
self = [super init];
if(self!=nil){
stack = [[NSMutableArray alloc] init];
}
return self;
}
-(void)push:(id)obj{
[stack addObject:obj];
}
-(id)pop{
id lastObj = [stack lastObject];
[stack removeLastObject];
return lastObj;
}
-(NSUInteger)size{
return stack.count;
}
-(id)peek{
return [[stack lastObject] copy];
}
-(BOOL)isEmpty{
return stack.count == 0;
}
-(NSString *)description{
NSMutableString *result = [[NSMutableString alloc] initWithString:@"["];
for (id s in stack) {
[result appendFormat:@"%@, ",[s description]];
}
if (stack.count>0) {
result = [[result stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", "]] mutableCopy];
}
[result appendString:@"]"];
return result;
}
@end
Lastly, this is how i verify my code is "working":
#import <Foundation/Foundation.h>
#import "Stack.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Stack *s1 = [[Stack alloc] init];
assert([s1 size]==0);
assert([s1 pop]==nil);
assert([s1 peek]==nil);
assert([s1 isEmpty]==YES);
NSLog(@"first set of tests passed");
[s1 push:[NSNumber numberWithInt:0]];
[s1 push:[NSNumber numberWithInt:3]];
[s1 push:[NSNumber numberWithInt:5]];
NSLog(@"%@",[s1 description]);
assert(![s1 isEmpty]);
assert(s1.size == 3);
assert([[s1 peek] isEqual:[NSNumber numberWithInt:5]]);
assert([[s1 pop] isEqual:[NSNumber numberWithInt:5]]);
assert(s1.size == 2);
assert([[s1 pop] isEqual:[NSNumber numberWithInt:3]]);
assert(![[s1 pop] isEqual:[NSNumber numberWithInt:3]]);
assert([s1 pop] == nil);
assert(s1.isEmpty);
NSLog(@"second set of tests passed");
}
return 0;
}