2
我使用這兩種遞歸的方法來找到特定的文件夾的iOS陣列沒有被完全
- (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);
你似乎沒有使用getPathForNamed:數組:這裏的。另外,當你正在做遞歸時,你將返回的數組添加到當前數組中。你是否想要添加返回數組的內容? – 2013-04-11 00:17:00
我使用了舊代碼,這就是爲什麼它看起來像'getPathForItemNamed:array:'沒有被調用,我的一個錯字。我添加數組的原因是它爲原始目錄中的每個子目錄添加一個數組。 – 2013-04-11 00:29:37