Hi all. 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..

Dani AI

Generated

Short answer for : build one NSDictionary per file, put those dictionaries into the array you already use for the table, and pass the selected NSDictionary to the detail view. Your current loop only overwrites a single set of variables for every file (and casts the whole array to an NSString), so you never retain per-file info.

Here is the typical workflow (illustrative Objective‑C):

  • Scan the directory and create an NSMutableArray of NSDictionary objects. Each dictionary should contain keys like name, path, size (NSNumber), type, created (NSDate) and modified (NSDate).
  • Use that array as your table data source.
  • In tableView:didSelectRowAtIndexPath: grab the dictionary at indexPath.row, set it on the detail view controller (e.g. a fileInfo property) and push/present the detail VC.
  • In the detail VC read values from the dictionary and format them for display (convert NSNumber file sizes with longLongValue, format dates with NSDateFormatter, etc).

Common pitfalls and checks:

  • Don’t cast the whole filenames array to NSString; extract the filename string for the current loop iteration.
  • Use the app’s documents path (via NSSearchPathForDirectoriesInDomains or URLsForDirectory:inDomains:) rather than a hardcoded Mac path.
  • attributesOfItemAtPath: returns NSNumber for NSFileSize and NSDate for date keys — convert/format before showing.
  • Skip directories (use fileExistsAtPath:isDirectory:) if you only want regular files.
  • If the directory scan may be slow, run it off the main thread and reload the table on the main thread when done.
  • If not using ARC, manage memory for the dictionaries/arrays you create.

This approach keeps each file’s properties grouped and makes it trivial to display the correct properties in the detail view when a row is tapped.

I need to learn

NSDictionary

..

Help me learn NSDictionary

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.