本文介绍了为什么NSDirectoryEnumerator在这里拾取隐藏文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要避免隐藏文件在这枚举,但是.DS_Store文件仍然被添加。 我放在NSLog检查,我得到输出 这可能是显而易见的,但我看不到。 NSDirectoryEnumerator * dirEnumerator; NSFileManager * fileManager = [[NSFileManager alloc] init]; dirEnumerator = [fileManager enumeratorAtURL:item includePropertiesForKeys:[NSArray array] options:NSDirectoryEnumerationSkipsPackageDescendants || NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil]; for(NSURL * urlItem in dirEnumerator){ //是项目隐藏? NSNumber * isHidden = nil; if([urlItem getResourceValue:& isHidden forKey:NSURLIsHiddenKey error:nil]){ if([isHidden isEqual:[NSNumber numberWithInt:1]]){ NSLog (@isHidden is 1); continue; } } 解决方案 ,真正的问题是,你使用错误的运算符来指定掩码: NSDirectoryEnumerationSkipsPackageDescendants || NSDirectoryEnumerationSkipsHiddenFiles 执行Boolean OR code> 1 ,这不是一个有用的选项掩码。您需要使用单个管道: NSDirectoryEnumerationSkipsPackageDescendants | NSDirectoryEnumerationSkipsHiddenFiles 这是按位 OR p> 老问答: 您需要实际请求您要查看的属性: dirEnumerator = [fileManager enumeratorAtURL:item includePropertiesForKeys:[NSArray arrayWithObject:NSURLIsHiddenKey] options:NSDirectoryEnumerationSkipsPackageDescendants || NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil];从 - [NSURL getResourceValue:forKey:error:] doc: 讨论 到nil是否未为URL定义所请求的资源值。在这种情况下,该方法仍然返回YES。 i need to avoid hidden files in this enumeration, but .DS_Store files are still being added.i put in the NSLog to check, and i am getting output there.there's probably something obvious, but i can't see it.NSDirectoryEnumerator *dirEnumerator; NSFileManager *fileManager = [[NSFileManager alloc] init]; dirEnumerator = [fileManager enumeratorAtURL:item includingPropertiesForKeys:[NSArray array] options:NSDirectoryEnumerationSkipsPackageDescendants || NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil]; for (NSURL *urlItem in dirEnumerator) { // is item hidden ? NSNumber *isHidden = nil; if ([urlItem getResourceValue:&isHidden forKey:NSURLIsHiddenKey error:nil]) { if ([isHidden isEqual:[NSNumber numberWithInt:1]]) { NSLog(@"isHidden is 1"); continue; } } 解决方案 Actually, the real problem is that you're using the wrong operator to specify the mask:NSDirectoryEnumerationSkipsPackageDescendants || NSDirectoryEnumerationSkipsHiddenFilesdoes Boolean OR, giving you 1, which isn't a useful options mask. You need to use the single pipe:NSDirectoryEnumerationSkipsPackageDescendants | NSDirectoryEnumerationSkipsHiddenFileswhich is bitwise OR.OLD ANSWER:You need to actually request the properties that you're going to look at:dirEnumerator = [fileManager enumeratorAtURL:item includingPropertiesForKeys:[NSArray arrayWithObject:NSURLIsHiddenKey] options:NSDirectoryEnumerationSkipsPackageDescendants || NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil];from the -[NSURL getResourceValue:forKey:error:] doc: Discussion value is set to nil if the requested resource value is not defined for the URL. In this case, the method still returns YES. 这篇关于为什么NSDirectoryEnumerator在这里拾取隐藏文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-27 19:44