Assuming that directoryPath points to the Documents directory of the app:
NSString *fileName = @"demo";
NSString *extension = @"txt";
NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
filePath = [filePath stringByAppendingPathExtension:extension];
I THINK that this is the way to do it, but I am not sure. Maybe there is an even better one. First I thought maybe I just create the file name with extension like this:
NSString *fileName = [NSString stringWithFormat:@"%@.%@", fileName, extension];
And then append it using -stringByAppendingPathComponent:
, but I bet this is a bad idea for some reason. Maybe because it hard-codes the dot separator for extensions. They will never change this because it would be dump as hell. But you never know. So...
Did I do it the right way?
Need support for old OS versions.
What about this?
NSString* fileNameExt = [NSString stringWithFormat:@"%@.%@", fileName, extension];
NSString *filePath = [directoryPath stringByAppendingPathComponent:fileNameExt];
精彩评论