在目录中获取文件(及其属性) [英] Getting files (and its attributes) in a directory

查看:120
本文介绍了在目录中获取文件(及其属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到一个目录中的文件列表,但我需要知道,如果该文件是一个目录,当它被最后一次修改,和offcourse它的名称:)。所以我尝试这样:

  NSFileManager * fm = [NSFileManager defaultManager]; 
NSURL * directoryURL = [NSURL fileURLWithPath:@/ Users / nacho4d / Desktop /];
NSArray * urls = [fm contentOfDirectoryAtURL:directoryURL
includePropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey,NSURLIsDirectoryKey,NSURLContentModificationDateKey,nil]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSError * error = nil;
NSString * name;
NSDate * modificationDate;
NSNumber * isDirectory;
for(NSURL * url in urls){
if(![url getResourceValue:& name forKey:NSURLNameKey error:& error]){
NSLog(@name:%@ ,[error localizedDescription]); error = nil;
}
if(![url getResourceValue:& modificationDate forKey:NSURLContentModificationDateKey error:& error]){
NSLog(@modificationDate:%@,[error localizedDescription]); error = nil;
}
if(![url getResourceValue:& isDirectory forKey:NSURLIsDirectoryKey error:& error]){
NSLog(@isDirectory:%@,[error localizedDescription]); error = nil;
}
NSLog(@%@%@%@,name,modificationDate,isDirectory);但是我总是得到(null)(null)$ {
}



< (null)
,没有错误,但没有数据;(



我不知道这里有什么问题?



PS:我知道 contentsOfDirectoryAtPath:error: attributesOfItemAtPath:error:是不是最好的方法,是吗?
我计划在可以包含大量文件的文件夹中使用它,所以我想这是最有效的方式。



编辑iOS5及更高版本



如@jeremyP指向,根据文档,方法未实现在iOS4中,但自iOS5及更高版本后,它似乎可以正常运行:


可用性

在iOS 5.0和更高版本中可用。(符号在iOS 4中存在,但不执行任何操作。)


是我最新的想法。现在在iOS 5
和6一切正常工作:)

解决方案

这不是你的问题的答案但是你的错误检查应该看起来像这样:

  if(![url getResourceValue:& name forKey:NSURLNameKey error:&错误])
{
NSLog(@name:%@,[error localizedDescription]); error = nil;
}

编辑

无法找出问题所在,只是重新阅读方法的文档。重要的句子是正在讨论的一句话:


此方法在iOS中未实现,因此不执行任何操作。



I want to get a list of files within a directory but I need to know if the file is a directory, when it was last modified, and offcourse its name :) . So I tried this:

NSFileManager *fm = [NSFileManager defaultManager];
NSURL *directoryURL = [NSURL fileURLWithPath:@"/Users/nacho4d/Desktop/"];
NSArray *urls = [fm contentsOfDirectoryAtURL:directoryURL 
                      includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey, NSURLContentModificationDateKey, nil] 
                                         options:NSDirectoryEnumerationSkipsHiddenFiles 
                                           error:nil];
NSError *error = nil;
NSString *name;
NSDate *modificationDate;
NSNumber *isDirectory;
for (NSURL *url in urls) {
    if (![url getResourceValue:&name forKey:NSURLNameKey error:&error]) {
        NSLog(@"name: %@", [error localizedDescription]);error = nil;
    }
    if (![url getResourceValue:&modificationDate forKey:NSURLContentModificationDateKey error:&error]) {
        NSLog(@"modificationDate: %@", [error localizedDescription]); error = nil;
    }
    if (![url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
        NSLog(@"isDirectory: %@", [error localizedDescription]); error = nil;
    }
    NSLog(@"%@ %@ %@", name, modificationDate, isDirectory);
}

However I always get (null) (null) (null), no errors but no data ;(

I wonder what is wrong here?

PS: I am aware of contentsOfDirectoryAtPath:error: and attributesOfItemAtPath:error: but I think that is not the best way of doing this, is it? I plan to use this in folders that can contain a large number of files so I would like to do this the most efficient way possible.

Edit for iOS5 and later

As @jeremyP pointed, According to the docs the method is unimplemented in iOS4 but since iOS5 and later it seems to work properly:

Availability
Available in iOS 5.0 and later. (Symbol is present in iOS 4, but performs no operation.)

This question was done when iOS4 was the latest I think. Now in iOS 5 and 6 Everything works as expected :)

解决方案

This isn't the answer to your question but your error checking should look like this:

if (![url getResourceValue:&name forKey:NSURLNameKey error:&error])
{
    NSLog(@"name: %@", [error localizedDescription]);error = nil;
}

Edit

Couldn't figure out what the problem was, but have just reread the documentation on the method. The important sentence is the one under discussion that says:

This method is unimplemented in iOS, so it performs no operation.

这篇关于在目录中获取文件(及其属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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