我正在将对象添加到模型中的 NSMutableArray 堆栈。这是界面:
@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);
}
是什么导致了这个错误?
最佳答案
您要添加的 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
.
关于objective-c - 简单的 addObject 到 NSMutableArray 方法给出 lldb 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464868/