我正在尝试显示具有不同数字因子(“质数”)的数组。但我总是得到 0,1,2,3,4,5,... 而不是给我 int 数字。
factors.text = @"";
int factorsNumber;
NSMutableArray *array;
array = [NSMutableArray arrayWithCapacity:5];
for (factorsNumber=1; factorsNumber<=prime; factorsNumber++) {
if (prime%factorsNumber == 0) {
[array addObject:[NSString stringWithFormat:@"%d", factorsNumber]];
}
}
for (int i = 0; i < [array count]; i++) {
[array replaceObjectAtIndex:1 withObject:@"4"];
NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]];
factors.text = [factors.text stringByAppendingString:temp];
}
最佳答案
替换
NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]];
与
NSString *temp = [NSString stringWithFormat:@"%d, ", [[array objectAtIndex:i] intValue]];
问题是您只打印数组索引,而不是值。
关于objective-c - 在 xcode 中显示数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11650370/