在 Objective-C 中检查目录中项目的属性
我编写了这段小代码来检查给定目录中有多少个子目录。它只检查第一级,有什么办法可以让它更简单吗?我添加了评论,也许更容易理解我的意图。谢谢你!
<前><代码> #import <基金会/Foundation.h> int main (int argc, const char * argv[]){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSFileManager *filemgr;
NSMutableArray *listOfFiles;
NSDictionary *listOfFolders;
NSDictionary *controllDir;
int i, count;
NSString *myPath;
filemgr = [NSFileManager defaultManager];
myPath = @"/";
// list the files in the given directory (myPath)
listOfFiles = [filemgr directoryContentsAtPath: myPath];
// count the number of elements in the array
count = [listOfFiles count];
// check them one by one
for (i = 0; i < count; i++)
{
// I need the full path
NSString *filePath =[NSString stringWithFormat:@"%@/%@", myPath, [listOfFiles objectAtIndex: i]];
// add every item with its attributes
listOfFolders = [filemgr attributesOfItemAtPath:filePath error:NULL];
// to avoid typo get the attribute and create a string
controllDir = [filemgr attributesOfItemAtPath:@"/" error:NULL];
NSString *toCheck = [NSString stringWithFormat:@"%@", [controllDir objectForKey:NSFileType]];
// the folder elements one by one
NSString *fileType = [NSString stringWithFormat:@"%@", [listOfFolders objectForKey:NSFileType]];
if([toCheck isEqualToString:fileType])
{
NSLog(@"NAME: %@ TYPE: %@" ,[listOfFiles objectAtIndex:i],[listOfFolders objectForKey:NSFileType]);
}
}
[pool drain];
return 0;
}
I have made this little code to check how many subdirectories are in a given directory. It checks only the first level, is there anyway I can make it simplier? I have added comments, maybe easier to understand my intention. Thank you!
#import < Foundation/Foundation.h > int main (int argc, const char * argv[]){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSFileManager *filemgr;
NSMutableArray *listOfFiles;
NSDictionary *listOfFolders;
NSDictionary *controllDir;
int i, count;
NSString *myPath;
filemgr = [NSFileManager defaultManager];
myPath = @"/";
// list the files in the given directory (myPath)
listOfFiles = [filemgr directoryContentsAtPath: myPath];
// count the number of elements in the array
count = [listOfFiles count];
// check them one by one
for (i = 0; i < count; i++)
{
// I need the full path
NSString *filePath =[NSString stringWithFormat:@"%@/%@", myPath, [listOfFiles objectAtIndex: i]];
// add every item with its attributes
listOfFolders = [filemgr attributesOfItemAtPath:filePath error:NULL];
// to avoid typo get the attribute and create a string
controllDir = [filemgr attributesOfItemAtPath:@"/" error:NULL];
NSString *toCheck = [NSString stringWithFormat:@"%@", [controllDir objectForKey:NSFileType]];
// the folder elements one by one
NSString *fileType = [NSString stringWithFormat:@"%@", [listOfFolders objectForKey:NSFileType]];
if([toCheck isEqualToString:fileType])
{
NSLog(@"NAME: %@ TYPE: %@" ,[listOfFiles objectAtIndex:i],[listOfFolders objectForKey:NSFileType]);
}
}
[pool drain];
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过这种方式来使用块:
值得注意的是,它只访问磁盘一次,并且不会花费任何时间来获取不需要的属性。一旦构建了
NSURL
,您就可以随时判断它是否是一个目录,因为它以 / 结尾(这是指定的行为)。这就是CFURLHasDirectoryPath()
所做的一切。它实际上并没有击中磁盘。You can get fancy with blocks this way:
Of note here is that it only hits the disk the one time, and it doesn't spend any time fetching unneeded attributes. Once you construct the
NSURL
, you can always tell if it's a directory because it ends in a / (this is specified behavior). That's allCFURLHasDirectoryPath()
is doing. It doesn't actually hit the disk.简要想法(从手机发布):
NSDirectoryEnumerator
。fileAttributes
将返回带有项目属性的NSDictionary
。NSDictionary
有一个fileType
方法,该方法将返回一个常量来指示项目的类型。NSFileTypeDirectory
的很好的常量,您可以使用它进行比较。Brief thoughts (posting from a cell phone):
NSDirectoryEnumerator
.fileAttributes
that will return anNSDictionary
with the item's attributes.NSDictionary
has afileType
method that will return a constant to indicate the kind of the item.NSFileTypeDirectory
you can use for comparison.这怎么样?
现在,
subdirs
数组包含所有直接子目录。How's this?
Now the
subdirs
array contains all of the immediate subdirectories.