iphone - Objective C - NSArray 和 For 循环结构

标签 iphone objective-c for-loop nsarray

工作阻碍了我学习 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/

相关文章:

iphone - 切换 View 时奇怪的 UI 行为(滑入)

javascript - JavaScript 中的最后一个 boolean 标志

javascript - 如何使javascript FOR LOOP在循环之前等待某些条件

ios - 如何自动启动iOS应用程序并禁止手动快速停止它

objective-c - Xcode - 调试单元测试目标

iphone - NSJSONSerialization 中没有返回

ios - TableViewCell 自动高度问题

c - 程序返回字符而不是字符串

iphone - UIImagePickerController ios 5 在 ipad 中使用 UIPopoverController 选择图像时出现问题

iphone - 从 UIImageView 打开 UIImagePicker