iPhone - 文件列表的属性

发布于 2024-10-01 17:45:03 字数 1689 浏览 1 评论 0原文

我有一个 mutableArray,它在 tableView 中显示文件列表。我已将这些文件存储在一个目录中。

我正在使用 NSfileManager 类和 attributesOfItemAtPath 方法来检索文件的一些属性,例如 fileSize、fileType..

在第二阶段,我尝试拥有一个显示详细信息的控制器在 tableView 中触摸时特定文件的属性。

问题是我不知道如何将 fileSize 和 fileType 等属性分别绑定到 NSDictionary 并使其显示在该特定文件的详细视图中。

这是我用于列出文件和属性的源代码。

- (void)listFiles {

    NSFileManager *fm =[NSFileManager defaultManager];
    NSError *error = nil;
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }
    directoryContent = [[NSMutableArray alloc] init];
    for (NSString *path in paths){
        NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
        [directoryContent addObject:documentsDirectory];
    }
}

-(void) willProcessPath {
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
    for (NSString *path in paths){
        filesPath = [parentDirectory stringByAppendingPathComponent:path];
        NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
        filesSize = [attrDir objectForKey:NSFileSize];
        filesName = (NSString *)directoryContent;
        filesType = [path pathExtension];
        createdDate = [attrDir objectForKey:NSFileCreationDate];
        modifiedDate = [attrDir objectForKey:NSFileModificationDate];
    }
}

请帮助我继续下一步..提前致谢..

I have an mutableArray which displays a list of files in a tableView. I have stored those files in a directory.

I am using NSfileManager class and attributesOfItemAtPath method to retrieve some properties of the files like fileSize, fileType..

In the second stage I am trying to have a detailView controller which displays the properties of the particular file when touched in the tableView.

The problem is I dont know how to bind those properties like fileSize and fileType separately to a NSDictionary and make it display in the detailView for that particular file.

Here is my source code for listing the files and to list the properties.

- (void)listFiles {

    NSFileManager *fm =[NSFileManager defaultManager];
    NSError *error = nil;
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }
    directoryContent = [[NSMutableArray alloc] init];
    for (NSString *path in paths){
        NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
        [directoryContent addObject:documentsDirectory];
    }
}

-(void) willProcessPath {
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
    for (NSString *path in paths){
        filesPath = [parentDirectory stringByAppendingPathComponent:path];
        NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
        filesSize = [attrDir objectForKey:NSFileSize];
        filesName = (NSString *)directoryContent;
        filesType = [path pathExtension];
        createdDate = [attrDir objectForKey:NSFileCreationDate];
        modifiedDate = [attrDir objectForKey:NSFileModificationDate];
    }
}

Please help me to proceed to the next step.. Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弱骨蛰伏 2024-10-08 17:45:03

尝试这样的事情:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self listFiles];
}



- (void)listFiles {

    NSFileManager *fm =[NSFileManager defaultManager];
    NSError *error = nil;
    NSString *parentDirectory = [self getDocumentsPath];
    NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];

    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }

    NSMutableArray *array = [[NSMutableArray alloc] init];
    self.directoryContent = array;
    [array release];

    for (NSString *path in paths){
        NSString *fullPath = [self getPathToFileInDocumentsDirectory:path];
        NSMutableDictionary *filePropertiesDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil]];
        [filePropertiesDictionary setObject:fullPath forKey:@"fullPath"];
        [filePropertiesDictionary setObject:path forKey:@"fileName"];
        [directoryContent addObject:filePropertiesDictionary];
    }
    NSLog(@"%@", directoryContent);
}

- (NSString *)getDocumentsPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];

    return documentsDirectoryPath;  
}
- (NSString *)getPathToFileInDocumentsDirectory:(NSString *)fileName {
    return [[self getDocumentsPath] stringByAppendingPathComponent:fileName];
}

Try something like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self listFiles];
}



- (void)listFiles {

    NSFileManager *fm =[NSFileManager defaultManager];
    NSError *error = nil;
    NSString *parentDirectory = [self getDocumentsPath];
    NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];

    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }

    NSMutableArray *array = [[NSMutableArray alloc] init];
    self.directoryContent = array;
    [array release];

    for (NSString *path in paths){
        NSString *fullPath = [self getPathToFileInDocumentsDirectory:path];
        NSMutableDictionary *filePropertiesDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil]];
        [filePropertiesDictionary setObject:fullPath forKey:@"fullPath"];
        [filePropertiesDictionary setObject:path forKey:@"fileName"];
        [directoryContent addObject:filePropertiesDictionary];
    }
    NSLog(@"%@", directoryContent);
}

- (NSString *)getDocumentsPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];

    return documentsDirectoryPath;  
}
- (NSString *)getPathToFileInDocumentsDirectory:(NSString *)fileName {
    return [[self getDocumentsPath] stringByAppendingPathComponent:fileName];
}
墨离汐 2024-10-08 17:45:03

为什么不创建一个具有以下属性的“FileDescriptor”类:

NSString *fileName;
NSString *fileSize;
NSString *fileType;
NSDate *createdDate;
NSDate *modifiedDate;

在遍历目录时,为每个文件创建此类的一个实例,并将其存储在数组(或字典,如果您愿意)中。然后,您可以使用该数组

a) 为 UITableViewCells 提供数据

b) 单击单元格时显示每个文件的详细视图。

如果你想使用字典(我认为这不太“干净”),你可以这样做:

NSDictionary *fileProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  fileSize, @"fileSize", 
  fileName, @"fileName", nil]; 

Why not create a class "FileDescriptor" with the following properties:

NSString *fileName;
NSString *fileSize;
NSString *fileType;
NSDate *createdDate;
NSDate *modifiedDate;

When iterating over your directory, you create an instance of this class for each file, and store that in an array (or a dictionary, if you like). Then you can use that array to

a) provide data for your UITableViewCells

b) show a detail view for each file when the cell is clicked.

Should you want to use a dictionary instead (which I think is a lot less "clean") you can do so like this:

NSDictionary *fileProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  fileSize, @"fileSize", 
  fileName, @"fileName", nil]; 
笛声青案梦长安 2024-10-08 17:45:03
- (void)listFiles {

    NSFileManager *fm =[NSFileManager defaultManager];
    NSError *error = nil;
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }
    directoryContent = [[NSMutableArray alloc] init];
    for (NSString *path in paths){
        NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
        [directoryContent addObject:documentsDirectory];
    }
}

-(void) willProcessPath {
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
    for (NSString *path in paths){
        filesPath = [parentDirectory stringByAppendingPathComponent:path];
        NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
         filesDirectory = [NSDictionary dictionaryWithObjectsAndKeys:filesSize, [attrDir objectForKey:NSFileSize],
                      filesName, (NSString *)directoryContent, filesType, [path pathExtension],
                      createdDate, [attrDir objectForKey:NSFileCreationDate],
                      modifiedDate, [attrDir objectForKey:NSFileModificationDate], nil];
        NSLog(@"%@", filesDirectory);
    }
}
- (void)listFiles {

    NSFileManager *fm =[NSFileManager defaultManager];
    NSError *error = nil;
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }
    directoryContent = [[NSMutableArray alloc] init];
    for (NSString *path in paths){
        NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
        [directoryContent addObject:documentsDirectory];
    }
}

-(void) willProcessPath {
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
    for (NSString *path in paths){
        filesPath = [parentDirectory stringByAppendingPathComponent:path];
        NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
         filesDirectory = [NSDictionary dictionaryWithObjectsAndKeys:filesSize, [attrDir objectForKey:NSFileSize],
                      filesName, (NSString *)directoryContent, filesType, [path pathExtension],
                      createdDate, [attrDir objectForKey:NSFileCreationDate],
                      modifiedDate, [attrDir objectForKey:NSFileModificationDate], nil];
        NSLog(@"%@", filesDirectory);
    }
}
堇年纸鸢 2024-10-08 17:45:03
#import <UIKit/UIKit.h>

@interface MyFilesList : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *directoryContent;
    IBOutlet UITableView *filesTable;
    IBOutlet NSNumber *filesSize;
    IBOutlet NSString *filesName;
    IBOutlet NSString *filesType;
    IBOutlet NSString *filesPath;
    IBOutlet NSDate *createdDate;
    IBOutlet NSDate *modifiedDate;
    NSMutableDictionary *filesDirectory;
}

@property (nonatomic, retain) NSMutableArray *directoryContent;
@property (nonatomic, retain) IBOutlet UITableView *filesTable;
@property (nonatomic, retain) IBOutlet NSNumber *filesSize;
@property (nonatomic, retain) IBOutlet NSString *filesName;
@property (nonatomic, retain) IBOutlet NSString *filesType;
@property (nonatomic, retain) IBOutlet NSString *filesPath;
@property (nonatomic, retain) IBOutlet NSDate *createdDate;
@property (nonatomic, retain) IBOutlet NSDate *modifiedDate;
@property (nonatomic, retain) NSMutableDictionary *filesDirectory;

- (void)listFiles;
- (void) willProcessPath;
@end
#import <UIKit/UIKit.h>

@interface MyFilesList : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *directoryContent;
    IBOutlet UITableView *filesTable;
    IBOutlet NSNumber *filesSize;
    IBOutlet NSString *filesName;
    IBOutlet NSString *filesType;
    IBOutlet NSString *filesPath;
    IBOutlet NSDate *createdDate;
    IBOutlet NSDate *modifiedDate;
    NSMutableDictionary *filesDirectory;
}

@property (nonatomic, retain) NSMutableArray *directoryContent;
@property (nonatomic, retain) IBOutlet UITableView *filesTable;
@property (nonatomic, retain) IBOutlet NSNumber *filesSize;
@property (nonatomic, retain) IBOutlet NSString *filesName;
@property (nonatomic, retain) IBOutlet NSString *filesType;
@property (nonatomic, retain) IBOutlet NSString *filesPath;
@property (nonatomic, retain) IBOutlet NSDate *createdDate;
@property (nonatomic, retain) IBOutlet NSDate *modifiedDate;
@property (nonatomic, retain) NSMutableDictionary *filesDirectory;

- (void)listFiles;
- (void) willProcessPath;
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文