当我有目录路径、文件名和扩展名时,如何构建完整且安全的文件路径?
假设directoryPath 指向应用程序的Documents 目录:
NSString *fileName = @"demo";
NSString *extension = @"txt";
NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
filePath = [filePath stringByAppendingPathExtension:extension];
我认为这是这样做的方法,但我不确定。也许还有更好的。首先,我想也许我只是创建具有如下扩展名的文件名:
NSString *fileName = [NSString stringWithFormat:@"%@.%@", fileName, extension];
然后使用 -stringByAppendingPathComponent:
附加它,但我敢打赌由于某种原因这是一个坏主意。也许是因为它对扩展的点分隔符进行了硬编码。他们永远不会改变这一点,因为它会被扔得一塌糊涂。但你永远不知道。那么...
我做得对吗?
需要对旧操作系统版本的支持。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这又如何呢?
What about this?