在Objective-C中检查目录中项目的属性 [英] Check the attribute of items in a directory in Objective-C

查看:47
本文介绍了在Objective-C中检查目录中项目的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这个小代码来检查给定目录中有多少子目录.它只检查第一级,无论如何我可以让它更简单吗?我添加了评论,也许更容易理解我的意图.谢谢!

<块引用>

 #import <基金会/Foundation.h >int main (int argc, const char * argv[]){

 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];//在此处插入代码...NSFileManager *filemgr;NSMutableArray *listOfFiles;NSDictionary *listOfFolders;NSDictionary *controllDir;int i,计数;NSString *myPath;filemgr = [NSFileManager defaultManager];myPath = @"/";//列出给定目录(myPath)中的文件listOfFiles = [filemgr directoryContentsAtPath: myPath];//计算数组中元素的个数计数 = [listOfFiles 计数];//一一检查for (i = 0; i < count; i++){//我需要完整路径NSString *filePath =[NSString stringWithFormat:@"%@/%@", myPath, [listOfFiles objectAtIndex: i]];//添加每个项目及其属性listOfFolders = [filemgr attributesOfItemAtPath:filePath 错误:NULL];//为了避免拼写错误,获取属性并创建一个字符串controllDir = [filemgr attributesOfItemAtPath:@"/" error:NULL];NSString *toCheck = [NSString stringWithFormat:@"%@", [controllDir objectForKey:NSFileType]];//文件夹元素一一NSString *fileType = [NSString stringWithFormat:@"%@", [listOfFolders objectForKey:NSFileType]];if([toCheck isEqualToString:fileType]){NSLog(@"NAME: %@ TYPE: %@" ,[listOfFiles objectAtIndex:i],[listOfFolders objectForKey:NSFileType]);}}[池排水];返回0;}

解决方案

简要想法(手机发帖):

  • 使用 NSDirectoryEnumerator.
  • 它有一个方法叫做 fileAttributes 将返回带有项目属性的 NSDictionary.
  • NSDictionary 有一个 fileType 方法,它将返回一个常量来指示项目的种类.
  • 有一个名为 NSFileTypeDirectory 的不错的常量,您可以用来进行比较.

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;
}

解决方案

Brief thoughts (posting from a cell phone):

  • use an NSDirectoryEnumerator.
  • it has a method called fileAttributes that will return an NSDictionary with the item's attributes.
  • NSDictionary has a fileType method that will return a constant to indicate the kind of the item.
  • there's a nice constant called NSFileTypeDirectory you can use for comparison.

这篇关于在Objective-C中检查目录中项目的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆