我正在尝试将数据保存到由 NSFileManager 创建的目录中,但是当我尝试检索数据和 NSLog 时,我得到空值。另外,当您创建目录时,这是否意味着您在指定的 url 路径中创建了一个文件夹?这是我正在使用的代码
NSError *error = nil;
NSFileManager *manager = [[NSFileManager alloc] init];
NSArray *urlsArray = [manager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSLog(@"%@", urlsArray);
// This will create a new url and append a photo title to the end
NSURL *url = [[urlsArray lastObject] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@", [self.recentPhoto objectForKey:FLICKR_PHOTO_TITLE]]];
NSLog(@"%@", url);
NSString *urlString = [NSString stringWithFormat:@"%@", [url absoluteString]];
NSLog(@"%@", urlString);
//create the directory
if(![manager fileExistsAtPath:urlString isDirectory:YES]){
BOOL success = [manager createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
NSLog(@"Error creating data path: %@", [error localizedDescription]);
}
}
//get url for a photo image and then store it.
NSURL *imageURL = [FlickrFetcher urlForPhoto:self.recentPhoto format:FlickrPhotoFormatLarge];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
[imageData writeToURL:url atomically:YES];
//get data to check if its stored
NSData *checkImageData = [[NSData alloc] initWithContentsOfURL:url];
//This returns null
NSLog(@"%@", checkImageData);
//this returns 0
NSLog(@"%d", [manager isReadableFileAtPath:urlString]);