工作阻碍了我学习 Objective C,但我现在又回来了,这让我发疯。
这是我的代码:
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 只是一个像这样的 NSArray,除了有 24 个对象:
NSArray* photoList = [NSArray arrayWithObjects:@"Photo 1.jpg",
@"Photo 2.jpg",
@"Photo 3.jpg",
@"Photo 4.jpg",nil];
它工作...它复制照片到相机胶卷...然后崩溃
2010-07-24 19:34:36.116 iCardz2go Poindexter[29662:207] * 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'* -[NSArray objectAtIndex:]: index 24 越界 [0 .. 23]'
我已经尝试过各种配置,例如
for (i=0;1<23; i++)
只是为了得到 2010-07-24 19:51:01.017 iCardz2go Poindexter[29908:207] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[NSInvocation invocationWithMethodSignature:]: 方法签名参数不能为 nil”
所以它正在读取 nil 并传递它。
我知道这将是一件我忘记的非常简单的事情。为什么它不跳出照片 23(计数)的循环?
非常感谢您的帮助!
最佳答案
为什么不尝试快速枚举?
for (NSString *photoFile in photoList) {
NSLog(@"%@", photoFile);
sendImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:photoFile
ofType:nil]];
UIImageWriteToSavedPhotosAlbum(sendImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), NULL);}
}
关于iphone - Objective C - NSArray 和 For 循环结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3324618/