Object C - NSArray and For loop structure

Work was on the road to learning Objective-C, but I returned to it now, and it infuriated me.

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, for example, with the exception of 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 frame ... and then resets using

2010-07-24 19: 34: 36.116 iCardz2go Poindexter [29662: 207] * Application terminated due to an uncaught exception 'NSRangeException', reason: '* - [NSArray objectAtIndex:]: index 24 outside [0 .. 23] ''

I tried various configurations like

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

2010-07-24 19: 51: 01.017 iCardz2go Poindexter [29908: 207] *** - "NSInvalidArgumentException", : "+ [NSInvocation invocationWithMethodSignature:]: nil '

, .

, , . Photo 23 (count)?

!

+5
5

C Objective-C :

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

initialization - , ; , . condition , ; condition true, . increment.

:

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

0 9. , , , :

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);
}

count ; , ( i < [photoList count].)

?

+12

?

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

  UIImageWriteToSavedPhotosAlbum(sendImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), NULL);}
}
+19

-— [photoList count] 1<23true. i < [photoList count]. , @Eimantas.

+4

:

for (i=0;[photoList count]; i++)

The condition is [photoList count]always true. However, I did not read all the discussions.

0
source

Change your for statement to (i = 0; i <[number of photolists]; i ++)

0
source

All Articles