Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - NSArray and For loop structure

Work got in the way of learning Objective C but i'm back at it now and this has been driving me crazy.

This is my code:

i=0;
    for (i=0;[photoList count]; i++) {
        NSLog(@"%i",i);
        NSLog(@"%@",[photoList objectAtIndex:i]);
        NSString *fileName = [photoList objectAtIndex:i];
        sendImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]];
        UIImageWriteToSavedPhotosAlbum(sendImage,self,@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),NULL);}

photoList is just an NSArray like so, except with 24 objects:

NSArray* photoList = [NSArray arrayWithObjects:@"Photo 1.jpg",
    @"Photo 2.jpg",
    @"Photo 3.jpg",
    @"Photo 4.jpg",nil];

It works... It copies the photos to the camera roll... and then crashes with

2010-07-24 19:34:36.116 iCardz2go Poindexter[29662:207] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSArray objectAtIndex:]: index 24 beyond bounds [0 .. 23]'

I've tried various configurations such as

for (i=0;1<23; i++)

only to get 2010-07-24 19:51:01.017 iCardz2go Poindexter[29908:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil'

So it's reading the nil and passing it.

I know its going to be something real simple that I've forgotten. Why doesn't it jump out the loop at Photo 23 (the count)?

Your help is greatly appreciated! P

like image 811
Paul Sommers Avatar asked Jul 24 '10 09:07

Paul Sommers


1 Answers

Why don't you try fast enumeration?

for (NSString *photoFile in photoList) {
  NSLog(@"%@", photoFile);
  sendImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
                               pathForResource:photoFile 
                                        ofType:nil]];

  UIImageWriteToSavedPhotosAlbum(sendImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), NULL);}
}
like image 153
Eimantas Avatar answered Oct 19 '22 02:10

Eimantas