Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS阵列未完全填充_Iphone_Ios_Objective C_Nsmutablearray - Fatal编程技术网

Iphone iOS阵列未完全填充

Iphone iOS阵列未完全填充,iphone,ios,objective-c,nsmutablearray,Iphone,Ios,Objective C,Nsmutablearray,我使用这两种递归方法来查找某个文件夹中的文件和目录路径 - (NSMutableArray *)getFilePathsFromDirectory:(NSString *)directory{ NSMutableArray *contents = [[NSMutableArray alloc] init]; NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:nil];

我使用这两种递归方法来查找某个文件夹中的文件和目录路径

- (NSMutableArray *)getFilePathsFromDirectory:(NSString *)directory{
NSMutableArray *contents = [[NSMutableArray alloc] init];
NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:nil];
for (NSString *file in arr) {
    BOOL isDir;
    [[NSFileManager defaultManager] fileExistsAtPath:[directory stringByAppendingPathComponent:file] isDirectory:&isDir];
    if (!isDir) {
        [contents addObject:[directory stringByAppendingPathComponent:file]];
    }
    else{
        [contents addObject:[directory stringByAppendingPathComponent:file]];
        [contents addObject:[self getFilePathsFromDirectory:[directory stringByAppendingPathComponent:file]]];
    }
}
return contents;
}

- (NSString *)getPathForItemNamed:(NSString *)name array:(NSMutableArray *)arr{
NSString *str;
if (name) {
    for (NSString *s in arr) {
        if ([s isKindOfClass:[NSString class]]) {
            if ([[s lastPathComponent] isEqualToString:name]) {
                return s;
            }
        }
    }
    for (NSMutableArray *aq in arr) {
        if ([aq isKindOfClass:[NSMutableArray class]]) {
            str = [self getPathForItemNamed:name array:aq];
            return str;
        }
    }
}
return str;
}
但问题是,在经过一定数量的子目录(3-5)之后,它停止返回任何路径并返回
(null)
。我觉得这与数组在返回之前没有填满所有目录有关。我这样称呼这些

NSMutableArray *paths = [self getContentsOfPaths:[self downloadsDir]];

path = [self getPathForItemNamed:[tableView cellForRowAtIndexPath:indexPath].textLabel.text array:paths];
NSLog(@"%@", path);

getPathForItemName:
方法有两个问题:

  • 如果无法按名称找到文件,则返回未初始化变量
    str
    的值。这是未定义的行为-初始化时需要将
    str
    设置为
    nil
    。事实上,您根本不需要
    str
    (请参阅下面的修复)
  • 当它发现它的第一个子目录时,它假定它要查找的文件必须在该子目录内,即使它不在该子目录内。无论
    getPathForItemName:
    的第一级递归调用返回什么,都将成为顶级调用的返回结果。这是不好的:如果您要查找的文件位于第二个子目录的子树中,您将永远找不到它
以下是修复方法的方法:

- (NSString *)getPathForItemNamed:(NSString *)name array:(NSMutableArray *)arr{
    if (!name) return nil;
    for (NSString *s in arr) {
        if ([s isKindOfClass:[NSString class]]) {
            if ([[s lastPathComponent] isEqualToString:name]) {
                return s;
            }
        }
    }
    for (NSMutableArray *aq in arr) {
        if ([aq isKindOfClass:[NSMutableArray class]]) {
            str = [self getPathForItemNamed:name array:aq];
            // Return something only when you find something
            if (str) return str;
        }
    }
    return nil; // You do not need str at all.
}

您似乎根本没有在这里使用getPathForName:array:。另外,在执行递归时,似乎要将返回的数组添加到当前数组中。你的意思是要添加返回数组的内容吗?我的旧代码已经更新,这就是为什么它看起来像
getPathForItemName:array:
没有被调用,这是我的一个输入错误。我添加数组的原因是它为原始目录中的每个子目录添加了一个数组。