[英]Increment a value in an int NSMutableArray
I've an NSMutableArray
as我有一个
NSMutableArray
作为
self.valuesArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt: 0],[NSNumber numberWithInt: 0],[NSNumber numberWithInt: 0],[NSNumber numberWithInt: 0], nil];
As you can see it's initialized with 0
now If I want to increment 3
at index 2
how can I do it?如您所见,它现在已初始化为
0
如果我想在索引2
处增加3
,我该怎么做? I was trying to do it like我试图这样做
self.valuesArray[2] = [self.valuesArray objectAtIndex:2]+3;
but that's not the right way.但这不是正确的方法。
I've also tried doing this我也试过这样做
[self.valuesArray[2] addObject:[NSNumber numberWithInt:[self.valuesArray[2] intValue]+3]];
but I got error that:但我得到的错误是:
Unrecognized selector sent to instance
无法识别的选择器发送到实例
You should do like this it will work.你应该这样做它会起作用。
int valueInc = [[self.valuesArray objectAtIndex:2] intValue] + 2;
NSNumber *numberValue = [NSNumber numberWithInt:valueInc];
[self.valuesArray replaceObjectAtIndex:2 withObject:numberValue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.