Consider the following two code samples:
NSData *imgData = UIImagePNGRepresentation(imgFull);
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"itemImg_%i.png", timestamp]]; //add our image to the path
[imgData writeToFile:fullPath atomically:YES];
and
NSData *imgData = UIImagePNGRepresentation(imgFull);
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"itemImg_%i.png", timestamp]]; //add our image to the path
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:imgData attributes:nil];
The second example requires an extra line of code and the initialization of an NSFileManager object, whereas the first example simply has the NSData object imgData write itself to a file. An additional advantage of the first example is that it can overwrite a pre-existing file that has the same name.
My question is: when creating new files, under what circumstances would you actually want to use NSFileManager and its method createFileAtPath:contents:attributes:?
The advantage of the
NSFileManagermethod is theattributesfield:This feature is unusual to use on iOS, but
NSFileManageris much older than iOS.BTW, the extra line you’re describing almost never comes up in real code. Either you already have a
fileManagervariable that you were using for other reasons, or you combine the two lines into one:And just one more. As you note:
Well, that’s an advantage or a disadvantage depending on what you want. If you mean “create this file, but don’t overwrite it if it already exists,” then the FM method is much more convenient. Maybe it’s an error to overwrite an existing file; this saves you the call to
fileExistsAtPath:. Maybe you want to create an empty file if it’s not there, but leave it alone if it is. Simple: pass[NSData data]as thecontentsvalue.So which is better depends on what problem you’re solving.