2012-12-22 70 views
0

請首先看看我的代碼。我想一些汽車對象添加到MutableArray * parkPlace(其中一個屬性屬於公園類)Objective C NSMutable Array無法添加項目

我期待的結果應該如下顯示:

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 

然而,actualy結果我得到的是像這樣:

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,它是空的......它dosent似乎任何東西推到堆棧... 有什麼建議?感謝

反正這裏是代碼: 公園類:

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

,這是我的車類:

#import <Foundation/Foundation.h> 

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

@end 


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

順便說一句,我試圖使用堆棧數據結構將汽車推到「堆棧」上,因爲沒有這樣的數據結構cture目標C,所以我已經實現了我自己的,這裏是Stack類:

#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 

回答

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 
+0

您可以通過使用實例變量而不是'init'和'dealloc'中的屬性引用來改進您的示例。從[高級內存管理編程指南](http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW1 ):*唯一不應該使用訪問器方法來設置實例變量的地方是初始化方法和'dealloc'。* – jlehr

+0

@jlehr好吧,這是可以爭論的。最好不要直接使用實例變量,因爲它們的* exact *內存管理內部部件是實現細節。 – 2012-12-22 17:42:26

+0

實例變量沒有「內存管理內部部件」,所以我不確定你的意思,但無論如何,你可能希望給出一些建議開發人員處理Apple文檔的優點的更多想法明確表示不應該這樣做。 – jlehr

3

您還沒有初始化parkPlace - 它仍然是零。 Objective-C允許您將消息默認發送至零,但它們不會執行任何操作。

如果我是你,我會在你的Park類的init方法中設置它。