[英]NSMutableArray addObject is nil?
I am new to iOS dev,here is my first app-calculator, But the NSMuteableArray "_numberArrayWaitingForCalculate" always be "nil",I don't know what to do??? 我是iOS开发人员的新手,这是我的第一个应用程序计算器,但是NSMuteableArray“ _numberArrayWaitingForCalculate”始终为“ nil”,我不知道该怎么办???
Here is the interface 这是界面
@interface demoViewController ()
@property (strong,nonatomic)NSString *valueString;
@property (strong,nonatomic)NSMutableArray *numberArrayWaitingForCalculate;
@end
here is the implement 1 这是工具1
@implementation demoViewController
@synthesize numberArrayWaitingForCalculate=_numberArrayWaitingForCalculate;
- (NSMutableArray *)numberWaitingForCalculate
{
if(!_numberArrayWaitingForCalculate)
_numberArrayWaitingForCalculate=[[NSMutableArray alloc]init];
return _numberArrayWaitingForCalculate;
}
here is the tapNumber method 这是tapNumber方法
- (IBAction)tapNumber:(UIButton *)numberButton {
if(LastButtonWasMode)
{
_valueString=@"";
LastButtonWasMode=NO;
}
NSString *numberAsString = numberButton.currentTitle;
_valueString=[_valueString stringByAppendingString:numberAsString];
result.text=[NSString stringWithFormat:@"%@",_valueString];
}
here is tapPlus method 这是tapPlus方法
- (IBAction)tapPlus:(id)sender {
[_numberArrayWaitingForCalculate addObject:[NSNumber numberWithInt:[_valueString intValue]]];
resultOfAllNumberInputBefore +=[_valueString intValue];
[self setMode:1];
} }
The following line should be using the property and not the instance variable. 以下行应使用属性,而不是实例变量。 ie you're not actually calling the getter that allocates the array.
也就是说,您实际上并没有在调用分配数组的getter。
Change this line: 更改此行:
[_numberArrayWaitingForCalculate addObject:[NSNumber numberWithInt:[_valueString intValue]]];
to: 至:
[self.numberArrayWaitingForCalculate addObject:[NSNumber numberWithInt:[_valueString intValue]]];
You created a getter that "lazy loads" the mutable array (meaning that you create it if it doesn't exist already. That's a valid approach. 您创建了一个“延迟加载”可变数组的getter(这意味着如果尚不存在则创建它。这是一种有效的方法。
However, if you do that, you need to ALWAYS use the getter. 但是,如果这样做,则必须始终使用吸气剂。 You're using the iVar directly (_numberArrayWaitingForCalculate).
您正在直接使用iVar(_numberArrayWaitingForCalculate)。 Don't do that.
不要那样做 Replace all instances of "_numberArrayWaitingForCalculate" with
[self numberArrayWaitingForCalculate]
except in the implementation of your getters/setters and probably your dealloc method. 用
[self numberArrayWaitingForCalculate]
替换“ _numberArrayWaitingForCalculate”的所有实例,但在实现getter / setter程序以及可能的dealloc方法中除外。
So your tapPlus method should read: 因此,您的tapPlus方法应为:
- (IBAction)tapPlus:(id)sender
{
[[self numberArrayWaitingForCalculate] addObject:[NSNumber numberWithInt:[_valueString intValue]]];
resultOfAllNumberInputBefore +=[_valueString intValue];
[self setMode:1];
}
EDIT: 编辑:
By the way, for something as lightweight as an empty mutable array, I think I would take a different approach. 顺便说一下,对于像一个空的可变数组这样轻量级的东西,我想我会采取不同的方法。 Rather than lazy-loading the array in a getter, I would create an init method for my class that created an empty mutable array and installed it in the iVar.
与其在getter中延迟加载数组,不如为我的类创建一个init方法,该方法创建一个空的可变数组并将其安装在iVar中。
Objects like view controllers can be initialized more than one way. 诸如视图控制器之类的对象可以通过多种方式初始化。 It might get initialized with
initWithNibName:bundle:
or with initWithCoder:
它可能使用
initWithNibName:bundle:
或initWithCoder:
What I do in that case is to create a method doInitSetup, and call it from both places. 在这种情况下,我要做的是创建方法doInitSetup,然后从两个地方调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.