开发者

iPhone - properties of a list of files

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开发者_JAVA技巧];
        modifiedDate = [attrDir objectForKey:NSFileModificationDate];
    }
}

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


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


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


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


#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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜