开发者

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

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...

开发者_Go百科

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜