将NSInteger添加到NSMutableArray

时间:2012-06-04 08:22:38

标签: ios cocoa-touch nsmutablearray

我在NSDocumentDirectory

中加载我的数组
self.images = [NSMutableArray new];  
for(int i = 0; i <= 100; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 
    NSLog(@"savedImagePath=%@",savedImagePath);
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        [images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 

        NSLog(@"file exists");
    } 
    NSLog(@"file does not exist");
} 

NSLog(@"Count : %d", [images count]);  

这是我实现我的数组的地方:

- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{

    view = [[UIImageView alloc] initWithImage:[images objectAtIndex:index]];
    return view;

}

我在NSInteger

中添加了上面数组的viewDidLoad
for (int x=0; x<+100;x++) {
        // add as NSString
        [images addObject:[NSString stringWithFormat:@"%d", x]];
        // add as NSNumber
        //[images addObject:[NSNumber numberWithInt:x]];
}

我在添加NSInteger时遇到问题。如何将NSInteger添加到NSMutableArray

我遇到了崩溃:

  

012-06-04 16:25:19.064 Yens_PhotoSlot [7543:1a303] - [__ NSCFNumber   size]:无法识别的选择器发送到实例0x9a49b20 2012-06-04   16:25:19.067 Yens_PhotoSlot [7543:1a303] *终止应用程序   未捕获的异常'NSInvalidArgumentException',原因:   ' - [__ NSCFNumber size]:发送到实例的无法识别的选择器   0x9a49b20'   * 第一次抛出调用堆栈:(0x1ab6052 0x1e92d0a 0x1ab7ced 0x1a1cf00 0x1a1cce2 0x7dbc86 0x2bc4b 0xaadc 0xaefc 0xb6f7 0xf04f 0xa348 0xbddf   0x27cc7 0x7bafbf 0x7bb2d4 0x7bb5d7 0x7bb785 0x9d827c 0x797c92 0x7979a2   0x799a98 0x724499 0x724584 0x139ee00 0x199f4f0 0x19ed833 0x19ecdb4   0x19ecccb 0x32e1879 0x32e193e 0x6f3a9b 0x2efd 0x2e65 0x1)终止   叫抛出异常

3 个答案:

答案 0 :(得分:16)

使用NSNumber

[yourMutableArray addObject:[NSNumber numberWithInt:yourIntValue]];

答案 1 :(得分:3)

添加NSNumber

[images addObject:[NSNumber numberWithInt:someInt];

答案 2 :(得分:2)

NSInteger 不是一个对象 - 它只是对32位的int或64位的long进行类型转换。由于 NSMutableArray 只能存储对象,因此需要先将整数包装到对象中,然后才能存储它。