我的数组没有添加对象

时间:2012-08-03 01:59:27

标签: objective-c arrays nsmutablearray

我想知道为什么myNumber没有被添加到myArray。对不起,如果答案真的很明显。我只是在关于数组的Objective-C书中坚持这一部分。

int main (int argc, char *argv[]) {
    @autoreleasepool {
        NSNumber *myNumber = [NSNumber numberWithInt: 5];
        NSMutableArray *myArray = [NSMutableArray array];

        [myArray addObject: myNumber];

        NSLog(@"%lu", [myArray count]);
        NSLog(@"%@", [myArray objectAtIndex: 0]);
    }
    return 0;
}

输出:

2012-08-02 21:53:30.827 prog[1889:707] 0
2012-08-02 21:53:30.829 prog[1889:707] (null)

1 个答案:

答案 0 :(得分:0)

NSNumber *myNumber2 = [NSNumber numberWithInt:5];
NSMutableArray *myArray = [NSMutableArray array];

[myArray addObject: myNumber2];

NSLog(@"%u", [myArray count]);
NSLog(@"%@", [myArray objectAtIndex: 0]);