How to create a complete and safe file path when I have a directory path, file name and extension?

Assuming directoryPath points to the application's Documents directory:

NSString *fileName = @"demo"; NSString *extension = @"txt"; NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName]; filePath = [filePath stringByAppendingPathExtension:extension]; 

I THINK that this is a way to do this, but I'm not sure. Maybe even better. At first I thought, maybe just creating a file name with an extension like this:

 NSString *fileName = [NSString stringWithFormat:@"%@.%@", fileName, extension]; 

And then add it with -stringByAppendingPathComponent: but I'm sure this is for some reason a bad idea. Maybe because it hardcodes the dot delimiter for extensions. They will never change it, because it will be a dump like hell. But you never know. So that...

Did I do it right?

Need support for older versions of the OS.

+4
source share
1 answer

How about this?

 NSString* fileNameExt = [NSString stringWithFormat:@"%@.%@", fileName, extension]; NSString *filePath = [directoryPath stringByAppendingPathComponent:fileNameExt]; 
+2
source

All Articles