Challenge Solution Chap 26


#1

This solution takes all the file names and their file attributes from the directory (path) “/tmp/”, and forms that information into a P-list then writes that data out to a file, then reads it back again from that file.

This solution employees the NSFileManager class.
***** This Solution has an Error **** Trying to resolve…p-list contains multiples of last file read in.

main.m

#import <Foundation/Foundation.h>
#import <Foundation/NSFileManager.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSString *pathName = @"/tmp";
        NSError *error = nil;
        
        NSFileManager *pathManager = [[NSFileManager alloc] init];
        NSArray *filesInPath = [pathManager contentsOfDirectoryAtPath:pathName
                                                                error:&error];
        NSLog(@"the path contains the following: %@", filesInPath);
        NSLog(@"the array filesInPath has %lu objects", [filesInPath count]);
        
        
        NSMutableArray *path = [[NSMutableArray alloc] init];
        NSMutableDictionary *file = [[NSMutableDictionary alloc] init];
        NSMutableString *pathAndFileName = [[NSMutableString alloc] init];
        
        for (int i = 0; i < ([filesInPath count] - 1); i ++) {

        /*  This is an alternative way to form the path string its untested however so I dont know if it works
          
            [pathAndFileName setString:[filesInPath objectAtIndex:i]];    // add file name
            [pathAndFileName insertString:@"/"];                          // now add "/" ccharacter
            [pathAndFileName insertString:pathName atIndex:0];            // now add path infront of file name
         */   
            [pathAndFileName setString];                            // add path
            [pathAndFileName appendString:@"/"];                             // now add "/" ccharacter
            [pathAndFileName appendString:[filesInPath objectAtIndex:i]];    // now add file name
            
            //
            NSLog(@"filesInPath index %d is %@", i, [filesInPath objectAtIndex:i]);
            NSLog(@"pathAndFileName looks like this:%@", pathAndFileName);

            error = nil;
            
            NSDictionary *fileAttributes = [pathManager attributesOfItemAtPath:pathAndFileName
                                                                         error:&error];
            NSLog(@"fileAttributes look like this:%@", fileAttributes);
            
            [file setObject:pathAndFileName
                     forKey:@"path and file name"];
            [file setObject:fileAttributes
                     forKey:@"file attributes"];
            
            [path addObject:file]; 
        }
        NSLog(@"\n\n\n\nthe path PList looks like this:%@",path);
        
        // Now write P-List to file /tmp/path.plist
        
        [path writeToFile:@"/tmp/path.plist"
                 atomically:YES];
        
        // Now confirm read from file /tmp/path.plist
        
        NSArray *readPath = [NSArray arrayWithContentsOfFile:@"/tmp/path.plist"];
        
        for (NSDictionary *d in readPath) {
            NSLog(@"\n\n\n\nThe path and file Name are:\n\n%@\n\n\nand its Attributes are\n\n%@\n\n\n\n", [d objectForKey:@"path and file name"], [d objectForKey:@"file attributes"]);
        }
    }
    return 0;
}

#2

This one seems to have resolved the problem.

main.m

#import <Foundation/Foundation.h>
#import <Foundation/NSFileManager.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSString *pathName = @"/tmp";
        NSError *error = nil;
        
        NSFileManager *pathManager = [[NSFileManager alloc] init];
        NSArray *filesInPath = [pathManager contentsOfDirectoryAtPath:pathName
                                                                error:&error];
        NSLog(@"the path contains the following: %@", filesInPath);
        NSLog(@"the array filesInPath has %lu objects", [filesInPath count]);
        
        
        NSMutableArray *path = [[NSMutableArray alloc] init];
        
        for (unsigned int i = 0; i < ([filesInPath count] - 1); i ++) {
            
            NSMutableString *pathAndFileName = [[NSMutableString alloc] init];
            
        /*  This is an alternative way to form the path string its untested however so I dont know if it works
          
            [pathAndFileName setString:[filesInPath objectAtIndex:i]];    // add file name
            [pathAndFileName insertString:@"/"];                          // now add "/" ccharacter
            [pathAndFileName insertString:pathName atIndex:0];            // now add path infront of file name
         */   
            [pathAndFileName setString];                            // add path
            [pathAndFileName appendString:@"/"];                             // now add "/" ccharacter
            [pathAndFileName appendString:[filesInPath objectAtIndex:i]];    // now add file name
            
            
            //NSLog(@"filesInPath index %d is %@", i, [filesInPath objectAtIndex:i]);
            //NSLog(@"pathAndFileName looks like this:%@", pathAndFileName);

            error = nil;
            
            NSDictionary *fileAttributes = [pathManager attributesOfItemAtPath:pathAndFileName
                                                                         error:&error];
            //NSLog(@"fileAttributes look like this:%@", fileAttributes);
            
            NSMutableDictionary *file = [[NSMutableDictionary alloc] init];
            
            [file setObject:pathAndFileName
                     forKey:@"path and file name"];
            [file setObject:fileAttributes
                     forKey:@"file attributes"];
            
            //NSLog(@"file looks like this: %@", file);
            
            [path insertObject:file atIndex:i]; 
            
            //NSLog(@"\n\npath looks like this:%@\n",path);

        }
        //NSLog(@"\n\n\n\nthe path PList looks like this:%@",path);
        
        // Now write P-List to file /tmp/path.plist
        
        [path writeToFile:@"/tmp/path.plist"
                 atomically:YES];
        
        // Now confirm read from file /tmp/path.plist
        
        NSArray *readPath = [NSArray arrayWithContentsOfFile:@"/tmp/path.plist"];
        
        NSLog(@"\n\n\n/tmp/path.plist is:\n\n %@\n\n\n", readPath);
        
        NSLog(@"\n\n\nthe files in directory: \"%@\" are:\n", pathName);
        
        for (NSDictionary *d in readPath) {
            NSLog(@"\n\n\n\npath and file Name:\n\n%@\n\n\nfile Attributes:\n\n%@\n\n\n\n", [d objectForKey:@"path and file name"], [d objectForKey:@"file attributes"]);
        }
    }
    return 0;
}