在xcode中显示数组

时间:2012-07-25 13:04:28

标签: objective-c arrays cocoa

我正在尝试显示具有不同数字因子的数组(“prime”)。但不是给我int数字,我总是得到0,1,2,3,4,5,....

        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];
        }

1 个答案:

答案 0 :(得分:1)

替换

NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]];

NSString *temp = [NSString stringWithFormat:@"%d, ", [[array objectAtIndex:i] intValue]];

问题是你只打印数组索引,而不是值。