2012-06-04 57 views
1

林加載我的陣列中的NSDocumentDirectory添加NSInteger的到一個NSMutableArray

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; 

} 

我與數組中的viewDidLoad添加NSInteger以上:

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。如何增加NSIntegerNSMutableArray?具有碰撞

林:

012-06-04 16:25:19.064 Yens_PhotoSlot [7543:1A303] - [__ NSCFNumber 尺寸]:無法識別的選擇發送到實例0x9a49b20 2012-06-04 16:25:19.067 Yens_PhotoSlot [7543:1A303] *終止應用程序由於 未捕獲的異常 'NSInvalidArgumentException',原因: ' - [__ NSCFNumber尺寸]:無法識別的選擇發送到實例 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)終止 稱爲拋出異常

+0

NSInteger不是類 'typedef int NSInteger; typedef unsigned int NSUInteger;' 看到它被聲明爲。 U應該轉換爲某些類,並且類對象可以添加到NSArray。 – dayitv89

回答

16

使用NSNumber

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

請發佈代碼,您將整數值添加到數組..您的代碼和您的問題似乎沒有關聯 – Krishnabhadra

+0

添加部分不負責崩潰。你正在調用[someIntValue size];這不存在。 – Umgre

+0

@Umgre指出,沒有在您的發佈代碼,可能會導致在日誌崩潰。 – Krishnabhadra

3

加入NSNumber

[images addObject:[NSNumber numberWithInt:someInt]; 
2

NSInteger不是一個對象 - 它只是簡單地將int轉換爲32位或長64位。由於NSMutableArray只能存儲對象,因此需要將整數封裝到對象中,然後才能存儲它。

+1

如何將整數包裝到對象中然後存儲它? – Bazinga

+1

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

相關問題