Please take a look of my code 1st. I am trying to add some Car objects to a MutableArray *parkPlace (which a property belongs to Park class)
My expecting result should be showing as below:
CarParkSim[2662:303] Parking: Car Number:1 At:3 o'clok Position:0
CarParkSim[2662:303] Parking: Car Number:2 At:4 o'clok Position:1
CarParkSim[2662:303] Parking: Car Number:3 At:5 o'clok Position:2
However, the actualy result I got is like this:
2012-12-22 12:13:46.085 CarParkSim[2662:303] Parking: Car Number:1 At:3 o'clok Position:0
2012-12-22 12:13:46.087 CarParkSim[2662:303] Parking: Car Number:2 At:4 o'clok Position:0
2012-12-22 12:13:46.088 CarParkSim[2662:303] Parking: Car Number:3 At:5 o'clok Position:0
I did check the NSmutablearray, it is empty…it dosent seem to push anything to the stack…
any suggestions? thanks
anyway, here is the code:
Park class:
#import <Foundation/Foundation.h>
#import "Stack.h"
#import "Car.h"
@interface Park : NSObject
@property(strong,nonatomic)NSMutableArray *parkPlace;
@property(strong,nonatomic)Car *car;
-(void)carMoveIntoPark:(Car *)car;
@end
#import "Park.h"
@implementation Park
-(void)carMoveIntoPark:(Car *)car
{
if ([self.parkPlace count] <= 2) {
Car *car1 = [[Car alloc] init];
car1.carNumber = 1;
car1.timeIO = 3;
[self.parkPlace push:car1];
int carNumber = car.carNumber;
int carTimer = car.timeIO;
NSUInteger parkPosition = [self.parkPlace count];
NSLog(@"Parking: Car Number:%d At:%d o'clok Position:%ld", carNumber, carTimer, parkPosition);
} else {
//to be implement later
}
}
@end
and here is my Main:
int main(int argc, const char * argv[])
{
Car *car1 = [[Car alloc] init];
car1.carNumber = 1;
car1.timeIO = 3;
Car *car2 = [[Car alloc] init];
car2.carNumber = 2;
car2.timeIO = 4;
Car *car3 = [[Car alloc] init];
car3.carNumber = 3;
car3.timeIO = 5;
Park *park = [[Park alloc] init];
[park carMoveIntoPark:car1];
[park carMoveIntoPark:car2];
[park carMoveIntoPark:car3];
and this is my Car class:
#import <Foundation/Foundation.h>
@interface Car : NSObject
@property(assign,nonatomic)int carNumber;
@property(assign,nonatomic)int timeIO;
@end
#import "Car.h"
@implementation Car
@end
By the way, I m trying to use stack data structure to push cars on to a ‘stack’, since there is no such data structure in objective c, so i have implemented my own, here is the stack class:
#import <Foundation/Foundation.h>
@interface NSMutableArray (Stack)
-(void) push:(id)item;
-(id)pop;
-(id)peek;
-(void)replaceTop:(id)item;
@end
#import "Stack.h"
@implementation NSMutableArray (Stack)
-(void)push: (id)item{
[self addObject:item];
}
-(id)pop {
id item = nil;
if ([self count] != 0) {
item = [self lastObject];
[self removeLastObject];
}
return item;
}
-(id)peek{
id item = nil;
if([self count] != 0) {
item= [self lastObject];
}
return item;
}
-(void) replaceTop:(id)item{
if([self count] == 0) {
[self addObject:item];
}else{
[self removeLastObject];
[self addObject:item];
}
}
@end
You don’t allocate memory anywhere for your array. That’s what constructors have been invented for: