1

Im loading my array in a 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]);  

Here is where I implement my array:

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

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

}

I add the NSInteger with the array above in the 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]];
}

I'm having trouble adding NSInteger. How to add NSInteger to an NSMutableArray?

Im having a crash:

012-06-04 16:25:19.064 Yens_PhotoSlot[7543:1a303] -[__NSCFNumber size]: unrecognized selector sent to instance 0x9a49b20 2012-06-04 16:25:19.067 Yens_PhotoSlot[7543:1a303] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber size]: unrecognized selector sent to instance 0x9a49b20' * First throw call stack: (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) terminate called throwing an exception

1
  • NSInteger is not a class typedef int NSInteger; typedef unsigned int NSUInteger; see it is declared as. U should convert to some Class and class object can add to NSArray. Commented Jun 4, 2012 at 8:37

3 Answers 3

16

Use NSNumber:

[yourMutableArray addObject:[NSNumber numberWithInt:yourIntValue]];
Sign up to request clarification or add additional context in comments.

19 Comments

Please post the code where you add integer values to array.. Your code and your question doesn't seem to relate
adding part is not responsible for crash. you're calling [someIntValue size]; which doesn't exist.
@Umgre pointed out, nothing in your posted code that could cause the crash in the log..
so what should I use and where should I insert it.
I guess your are calling "length" method in somewhere. so it doesn't crash with NSString but crash with NSNumber.
|
3

Add NSNumber:

[images addObject:[NSNumber numberWithInt:someInt];

Comments

2

NSInteger isn't an object -- it's simply typecast to int on 32-bit or long on 64-bit. Since NSMutableArray can only store objects, you need to wrap the integer into an object before you can store it.

2 Comments

how to wrap the integer into an object and then store it?
[yourMutableArray addObject:[NSNumber numberWithInt:yourIntValue]];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.