nsdata writeToURL:atomically:返回true但我的目录为null

时间:2013-10-04 02:45:21

标签: ios objective-c nsdata writetofile

我一直在看这几个小时,似乎无法弄清楚我的问题。这是我的代码

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

这是我的错误日志

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)

基本上我的NSData writeToFile:atomically:返回true,但是当我检查目录“stanfordphotos”时,NSFileManager方法返回一个空数组。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

以上CodaFi的评论很棒,如果你想做的话也是如此,你可以这样做。虽然我删除了这一行。如果需要的话。 NSArray* cacheDirectory = [fileManager contentsOfDirectoryAtPath:[pathURL absoluteString] error:nil];因此,尽管有数组,你可以直接将文件名作为字符串获取: -

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