enumerate Files and Folders(遍历)

1。对指定目录的浅遍历

   - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error 

e.g.

- (void) actionEnumerate{ 

    NSFileManager *fileManager = [[NSFileManager alloc] init];

    NSString *bundleDir = [[NSBundle mainBundle] bundlePath];

    NSError *error = nil;

    NSArray *bundleContents = [fileManager  contentsOfDirectoryAtPath:bundleDir  error:&error];  

    if ([bundleContents count] > 0 && error == nil){

        NSLog(@"Contents of the app bundle = %@", bundleContents);

    }

    else if ([bundleContents count] == 0 && error == nil){

        NSLog(@"Call the police! The app bundle is empty.");

    }

    else {

        NSLog(@"An error happened = %@", error);

    }

}

2。对指定目录的浅遍历(并能获取到结果中每一数据项的额外信息,如是否文件,创建时间等)

   - (NSArray *)contentsOfDirectoryAtURL:(NSURL *)url includingPropertiesForKeys:(NSArray *)keys

                             options:(NSDirectoryEnumerationOptions)mask error:(NSError **)error 

参数

   includingPropertiesForKeys: NSURLIsDirectoryKey / NSURLIsReadableKey / NSURLCreationDateKey

                               NSURLContentAccessDateKey / NSURLContentModificationDateKey

   options:0 //显示全部文件

           NSDirectoryEnumerationSkipsHiddenFiles //忽略隐藏文件

3。获取文件属性 (NSURL类的实例方法)

   - (BOOL)getResourceValue:(out id *)value forKey:(NSString *)propertyKey error:(out NSError **)error


e.g.

//获取指定文件夹(.app)下的内容(返回结果中的每一项都是NSURL,且带有各种属性,如创建日期,最后修改日期等)

- (NSArray *) contentsOfAppBundle{

    NSFileManager *manager = [[NSFileManager alloc] init];

    NSURL *bundleDir = [[NSBundle mainBundle] bundleURL];

    //各种属性

    NSArray *propertiesToGet = @[

                                NSURLIsDirectoryKey,

                                 NSURLIsReadableKey,

                                 NSURLCreationDateKey,

                                 NSURLContentAccessDateKey,

                                 NSURLContentModificationDateKey

                                 ];


    NSError *error = nil;

    NSArray *result = [manager contentsOfDirectoryAtURL:bundleDir includingPropertiesForKeys:propertiesToGet

                                                options:0  error:&error];    

    if (error != nil){

        NSLog(@"An error happened = %@", error);

    }    

    return result;

}


- (NSString *) stringValueOfBoolProperty:(NSString *)paramProperty  ofURL:(NSURL *)paramURL{

    NSNumber *boolValue = nil;

    NSError *error = nil;

    [paramURL getResourceValue:&boolValue forKey:paramProperty  error:&error];

    if (error != nil){

        NSLog(@"Failed to get property of URL. Error = %@", error);

    }

    return [boolValue isEqualToNumber:@YES] ? @"Yes" : @"No";   

}

- (NSString *) isURLDirectory:(NSURL *)paramURL{

    return [self stringValueOfBoolProperty:NSURLIsDirectoryKey ofURL:paramURL];

}

- (NSString *) isURLReadable:(NSURL *)paramURL{

    return [self stringValueOfBoolProperty:NSURLIsReadableKey ofURL:paramURL];

}


- (NSDate *) dateOfType:(NSString *)paramType inURL:(NSURL *)paramURL{

    NSDate *result = nil;

    NSError *error = nil;

   [paramURL getResourceValue:&result forKey:paramType  error:&error];

    if (error != nil){

        NSLog(@"Failed to get property of URL. Error = %@", error);

    }

    return result;

}


- (void) printURLProperties:(NSURL *)paramURL{    

    NSLog(@"Item name = %@", [paramURL lastPathComponent]);

    NSLog(@"Is a Directory? %@", [self isURLDirectory:paramURL]);

    NSLog(@"Is Readable? %@", [self isURLReadable:paramURL]);

    NSLog(@"Creation Date = %@",

          [self dateOfType:NSURLCreationDateKey inURL:paramURL]);

    NSLog(@"Access Date = %@",

          [self dateOfType:NSURLContentAccessDateKey inURL:paramURL]);

    NSLog(@"Modification Date = %@",

          [self dateOfType:NSURLContentModificationDateKey inURL:paramURL]);

}


-(void)actionEnumerate {

    NSArray *itemsInAppBundle = [self contentsOfAppBundle];

    for (NSURL *item in itemsInAppBundle){

        [self printURLProperties:item];

    }

}


postscript: 
    1。上述罗列的方法是属于浅遍历,若要深入遍历则需用

       enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值