有人可以通过循环NSMutableArray
告诉我如何检索值吗?我的代码基本上为数组添加了整数,如下所示:
NSMutableArray *ptr = [[NSMutableArray alloc] init];
[ptr addObject:[NSNumber numberWithInt:1]];
[ptr addObject:[NSNumber numberWithInt:2]];
[ptr addObject:[NSNumber numberWithInt:3]];
// How to retrieve them as integers?
我正在尝试从数组中检索每个数字并将它们总计为总值。
答案 0 :(得分:5)
其实非常简单:
int totalValue = 0;
for(NSNumber *number in myArray) // Use fast enumeration to iterate through the array
{
totalValue += [number intValue];
}
答案 1 :(得分:0)
我也是新手,所以我的回答可能不对,但试试这个:
int sum = 0;
for (int i = 0, i < [ptr count], i++){
int value = [[ptr objectAtIndex:i] intValue] //you get the int, integerValue is applicable for NSInteger
sum = sum + value;//you adding values
}