Search code examples
iosobjective-cnsdatawritetofile

nsdata writeToURL:atomically: returns true but my directory comes up null


I have been looking at this for hours and can't seem to figure out my problem. Here is my code

NSFileManager* fileManager = [[NSFileManager alloc] init];
                    NSArray* urls = [fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];

                NSArray* directory = [fileManager contentsOfDirectoryAtURL:urls[0] includingPropertiesForKeys:@[@"NSCreatingDateKey"] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
                NSLog(@"%@", directory);
                NSURL* pathURL = [[urls objectAtIndex:0] URLByAppendingPathComponent:@"stanfordpictures/"];
                if ([directory count] < 3) {
                    [fileManager createDirectoryAtURL:pathURL withIntermediateDirectories:NO attributes:nil error:nil];
                }
                NSString* pictureString = [NSString stringWithFormat:@"picture%d.png", self.cacheCounter];
                NSURL* pictureURL = [pathURL URLByAppendingPathComponent:pictureString];
                NSURL* absolutePictureURL = [pictureURL absoluteURL];
                NSLog(@"%@", pictureURL);
               NSData *cacheImageData = UIImagePNGRepresentation(image);
                if ([cacheImageData writeToURL:pictureURL atomically:YES])
                {
                    NSLog(@"It worked");
                    NSArray* cacheDirectory = [fileManager contentsOfDirectoryAtPath:[pathURL absoluteString] error:nil];
                    NSLog(@"%@", cacheDirectory);
                }
                else
                {
                    NSLog(@"An error occurred");
                }

And here is my error log

2013-10-03 22:36:48.920 Assignment4[14754:c07] (
"file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/com.DelNano.Dom.Assignment4/",
"file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/Snapshots/",
"file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/stanfordpictures/"

)

file://localhost/Users/ddelnano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/683F512B-7FFC-4C3E-9526-715B09AABD56/Library/Caches/stanfordpictures/picture0.png
2013-10-03 22:36:49.031 Assignment4[14754:c07] It worked
2013-10-03 22:36:49.032 Assignment4[14754:c07] (null)

So basically my NSData writeToFile:atomically: is returning true but when I check the directory "stanfordphotos" the NSFileManager method returns a null array. Any help would be greatly appreciated.


Solution

  • The above comment of CodaFi is great, Also the samething if you want to do you can do like this below. Though i have removed this line. if its required. NSArray* cacheDirectory = [fileManager contentsOfDirectoryAtPath:[pathURL absoluteString] error:nil];So that you can directly fetch the name of file as a string inspite of an array, :-

        NSFileManager* fileManager = [[NSFileManager alloc] init];
        NSArray* urls = [fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
    
        NSArray* directory = [fileManager contentsOfDirectoryAtURL:urls[0] includingPropertiesForKeys:@[@"NSCreatingDateKey"] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
        NSLog(@"%@", directory);
        NSURL* pathURL = [[urls objectAtIndex:0] URLByAppendingPathComponent:@"stanfordpictures/"];
       if ([directory count] < 3) {
            [fileManager createDirectoryAtURL:pathURL withIntermediateDirectories:NO attributes:nil error:nil];
        }
        NSString* pictureString = @"hussain_2156.pptx";//[NSString stringWithFormat:@"picture%d.png", self.cacheCounter];
        NSURL* pictureURL = [pathURL URLByAppendingPathComponent:pictureString];
        NSLog(@"%@", pictureURL);
        NSData *cacheImageData = [NSData dataWithContentsOfFile:@"/Users/hussain/Desktop/hussain_2156.pptx"];
        if ([cacheImageData writeToURL:pictureURL atomically:YES])
        {
            NSLog(@"It worked");
            NSString *file=nil;
            NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:[pathURL path]];
            while(file =[dirEnum nextObject])
            {
                NSLog(@"%@",file);
            }
        }
        else
        {
    
            NSLog(@"An error occurred");
        }