views:

466

answers:

3

Hi everyone,

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

+6  A: 

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);}
}
Eimantas
Thank you! I hadnt heard of fast enumeration until now... so much to learn.But using your code it crashes before it writes to camera roll with the same error:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil'
Paul Sommers
That is to say with the loop (and theres a mistake in the code printed above it is actually i < [photoList count]... it goes through the loop, writes the images, then crashes.
Paul Sommers
+4  A: 
Marcelo Cantos
Thank you so much... it was i < [photoList count] I don't know what happened in pasting. But that doesn't fix it.
Paul Sommers
Using i<=[photoList count] it runs and then crashes with:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 24 beyond bounds [0 .. 23]'Does that help narrow the problem?
Paul Sommers
+3  A: 

For loops in C and Objective-C look like this:

for (initialization; condition; increment) {
    // body
}

initialization is where you set up the loop; it's where you tell it what to start with. condition is tested for each iteration, including the first; if the condition evaluates to true, the body of the loop is executed. At the end of each iteration, increment is evaluated.

So:

for (int i = 0; i < 10; i++) {
    printf("%i\n", i);
}

Will print the numbers 0 through 9. What you probably want for yours is:

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

Note the assignment of count outside the loop; it's simply an optimization so the loop doesn't have to send an extra message for each iteration (you could just as easily do i < [photoList count].)

Does that help?

Jonathan Grynspan
Thank you also. Still crashing with: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil' It's driving me nuts becaise I don't know enough to interpret the error message.
Paul Sommers
Does your class actually respond to `-savedPhotoImage:didFinishSavingWithError:contextInfo:`? You need to make sure that's implemented.
Jonathan Grynspan
THANK YOU! That was the problem! I remember commenting it out testing... something... a couple of days ago and low and behold it was still commented out!!!!But because I was playing with NSArray for the first time, and getting real advanced (LOL!) by putting it in a loop I automatically assumed the problem was there.Your help and those above is enormously appreciated by me and other newbies. Usually I can find the answer just by searching but this had me stumped.Thank you all! Hopefully one day I can do the same for people.
Paul Sommers