What happens if a file already exists and you try writing to it?
Will it get over written or will write fail?
NSData * pdfData = [[NSData alloc] initWithData开发者_运维问答:[PDFImageConverter convertImageToPDF: [image image]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * filename = sharedManager.localFileName; //file name already exists
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:filename];
[pdfData writeToFile:pdfPath atomically:YES];
[pdfData release];
It will get overwritten if writing succeeds. Note that if atomically is set to YES, the original file won't get damaged if writing fails. If atomically is set to NO, it will.
If you want to check whether file exists or not and want to remove before storing another pdf, then following is the code
BOOL success = [FileManager fileExistsAtPath:zipPath];
if(success){
[FileManager removeItemAtPath:zipPath error:&error];
}
In this particular case, it will overwrite. But there are some other frameworks' API, write data to file existing will fail. For example,
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:toFile:options:completionHandler:];
So it different from API to API.
精彩评论