Objective C NSMutable Array无法添加项目

时间:2012-12-22 12:36:13

标签: objective-c object interface nsmutablearray stack

请先查看我的代码。我试图将一些Car对象添加到MutableArray * parkPlace(属性属于Park类)

我期待的结果应如下所示:

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

然而,我得到的实际结果是这样的:

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

我确实检查过NSmutablearray,它是空的......似乎把任何东西都推到了堆栈...... 有什么建议?感谢

无论如何,这是代码: 公园课:

#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

这是我的主要内容:

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];

这是我的Car类:

#import <Foundation/Foundation.h>

@interface Car : NSObject
@property(assign,nonatomic)int carNumber;
@property(assign,nonatomic)int timeIO;

@end


#import "Car.h"
@implementation Car
@end

顺便说一下,我试图使用堆栈数据结构将汽车推送到'堆栈',因为在目标c中没有这样的数据结构,所以我已经实现了我自己的,这里是堆栈类:< / p>

#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

2 个答案:

答案 0 :(得分:9)

您不为阵列的任何位置分配内存。这就是为什么发明构造函数:

@implementation Park

- (id)init
{
    if ((self = [super init])) {
        parkPlace = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc
{
    // The destructor needs to be implemented as well
    [parkPlace release];
    [super dealloc];
}

// Other parts of the implementation here

@end

答案 1 :(得分:3)

你没有初始化parkPlace - 它仍然是零。 Objective-C允许您默认将消息发送到nil,但它们不执行任何操作。

如果我是你,我会在你的Park类的init方法中设置它。