目标C代码的部分被Xcode“跳过”而没有错误

时间:2014-07-18 03:34:24

标签: ios objective-c for-loop

我遇到了Xcode 5.1.1“跳过”我的代码部分的问题。我已经添加了NSLog调试标记,我可以看到它正在Debug a,而不是Debug b,但运行Debug c并跟随。我没有错误,应用程序在模拟器中运行正常,除了没有跳过的代码。我不确定这里有什么问题,因为本身没有错误或问题,所以我很难找到答案。我刚出现的东西似乎不适用于我的问题。

第一部分加载mainBundle目录中的前4个.jpg,然后转换并缩放它们以供UIImage使用。这部分工作正常。我使用相同的代码然后从同一个mainBundle目录加载前10个.png图标。这是被跳过的。我在下面列出了前面的工作代码和下面的“跳过”代码。

    NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
    NSString *imageFileName;
    int i=0;
    _profileImages = [NSMutableArray arrayWithObjects: @"", @"", @"", @"", nil];


    for (NSURL *fileURL in enumerator) {
        [fileURL getResourceValue:&imageFileName forKey:NSURLNameKey error:nil];

        NSNumber *isDirectory;
        [fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];

        if (i<4){
            if ([imageFileName hasSuffix:@"en.jpg"]){
                NSURL *fqImageFileName = [NSURL URLWithString:imageFileName relativeToURL:bundleURL];
                [_profileImages replaceObjectAtIndex:i withObject:fqImageFileName];
                i++;
            }
        }
    }

    //convert image file name into image binary and scale

    for (int j=0; j< _profileImages.count; j++){
        NSData *imageData = [NSData dataWithContentsOfURL:_profileImages[j]];
        UIImage *myimageRaw = [UIImage imageWithData:imageData];
        CGSize imageSize = myimageRaw.size;
        float imageHeight = imageSize.height;
        UIImage *myimage = [UIImage imageWithData:imageData scale:imageHeight/284];
        [_profileImages replaceObjectAtIndex:j withObject:myimage];
    }

    NSString *iconFileName;
    int k=0;
    _profileIcons = [NSMutableArray arrayWithObjects: @"", @"", @"", @"",  @"", @"", @"", @"", @"", @"", nil];

    NSLog(@"Debug a"); //last location code executes

    for (NSURL *fileURL in enumerator) {

        NSLog(@"Debug b"); //not executing

        [fileURL getResourceValue:&iconFileName forKey:NSURLNameKey error:nil];

        NSNumber *isDirectory;
        [fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];


        if (k<10){
            if ([iconFileName hasSuffix:@"-128.png"]){
                NSURL *fqIconFileName = [NSURL URLWithString:iconFileName relativeToURL:bundleURL];
                [_profileIcons replaceObjectAtIndex:k withObject:fqIconFileName];
                k++;
            }
        }
    }

    NSLog(@"Debug c"); //resumes execution

修改 好。所以我开始工作了。但我不确定我理解为什么。看起来我为你们所有人留下了代码的关键部分。枚举器声明为:

    NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:bundleURL
                                          includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
                                                             options:NSDirectoryEnumerationSkipsHiddenFiles
                                                        errorHandler:^BOOL(NSURL *url, NSError *error)
                                         {
                                             if (error) {
                                                 NSLog(@"[Error] %@ (%@)", error, url);
                                                 return NO;
                                             }

                                             return YES;
                                         }];

当我将NSDirectoryEnumerator的另一个实例声明为“enumerator2”时,它的声明完全相同。我想我应该能够使用相同的实例,因为我在两个文件集的相同目录中查找。那么为什么我不能同时使用相同的实例呢?每次我需要迭代同一个目录时,有没有比创建新实例更好的方法呢?

2 个答案:

答案 0 :(得分:0)

根据您的编辑,问题很简单 - 枚举器只能枚举一次。

来自NSEnumerator的文档(NSDirectoryEnumerator的基类):

  

您将nextObject重复发送到新创建的NSEnumerator对象,以使其返回原始集合中的下一个对象。当集合用尽时,将返回nil。在枚举器耗尽之后,您无法“重置”枚举器。要再次枚举集合,您需要一个新的枚举器。

正如您所做的那样,创建第二个枚举器是正确的解决方案。

答案 1 :(得分:0)

  

那么为什么我不能同时使用相同的实例?

因为枚举器本身就包含状态 - 枚举器的全部意义在于它会记住您在枚举的集合中的距离。一旦你到达终点,枚举器就完成了。要再次枚举整个集合,您需要一个新的枚举器。

除此之外,您似乎正在将一个枚举器与&#34;快速枚举混合在一起。&#34;我不确定这是否有效,但无论哪种方式,您都可以通过对集合使用快速枚举来更接近您想要的内容(并简化代码)。而不是从文件管理器中获取枚举器,而是获取目录的内容:

NSArray *contents = contentsOfDirectoryAtURL:bundleURL 
                  includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
                                     options:NSDirectoryEnumerationSkipsHiddenFiles
                                       error:&error

由于contents是一个数组,而不是一个枚举器,你可以在循环中多次重复使用它:

for (NSURL *fileURL in contents) {
    //...
}
//...
for (NSURL *fileURL in contents) {
    //...
}