2012-11-20 51 views
0

我在我的模型中添加對象到NSMutableArray堆棧。這裏的接口:簡單的addObject到NSMutableArray方法給出lldb錯誤

@interface calcModel() 
@property (nonatomic, strong) NSMutableArray *operandStack; 

@end 

和實現:

@implementation calcModel 
@synthesize operandStack = _operandStack; 

- (NSMutableArray *)operandStack; 
{ 
if (_operandStack == nil) _operandStack = [[NSMutableArray alloc]init]; 
return _operandStack; 
} 

這ADDOBJECT法正常工作:

- (void)pushValue:(double)number; 
{ 
[self.operandStack addObject:[NSNumber numberWithDouble:number]]; 
NSLog(@"Array: %@", self.operandStack); 
} 

但是這一個崩潰的應用程序,只是說 'LLDB' 在日誌中:

- (void)pushOperator:(NSString *)operator; 
{ 
[self.operandStack addObject:operator]; 
NSLog(@"Array: %@", self.operandStack); 
} 

什麼是導致這個錯誤?

+0

如果您使用當前的XCode版本開始使用Objective-C,請參閱http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_4.html:@synthesize不再是需要。 – alecail

+0

如果它真的只是**說「lldb」,那麼你可能會在該行設置一個斷點......或者你正在停止一個異常斷點。 – borrrden

回答

3

您要添加的NSString大概是nil。這樣做:

- (void)pushOperator:(NSString *)operator { 
    if (operator) { 
     [self.operandStack addObject:operator]; 
     NSLog(@"Array: %@", self.operandStack); 
    } else { 
     NSLog(@"Oh no, it's nil."); 
    } 
} 

如果是這樣的話,弄清楚它爲什麼nil和解決這個問題。或者在添加之前檢查它。

第一種方法不會崩潰的原因是,因爲沒有不能用於初始化NSNumber的double值,所以它永遠不會是nil

+1

+1,但請注意,方法頭之後的分號不一定是語法錯誤;有些人更喜歡它在實現文件中,以便在.h和.m文件之間複製和粘貼更容易(只需雙擊該行並執行它)。 – Tim

相關問題