要进入我的应用程序文档文件夹,我使用以下代码:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
我想访问这个文件夹,但是:
~/Library/Mobile Documents
我怎样才能轻松地将其作为路径值访问?我可以用类似的方式做到这一点吗?
最佳答案
使用常量访问系统提供的目录的好处是,如果 Apple 决定更改结构,您的应用程序仍然可以工作。像 ~/Library/Mobile Documents
这样的硬编码很脆弱。
但是,您可以通过 NSLibraryDirectory
常量访问具有相同 NSSearchPathForDirectoriesInDomain
的库目录。然后,您应该只附加 Mobile Documents
目录路径。
// Set the NO to YES to get the full path, not the ~ version.
NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, NO) lastObject];
path = [path stringByAppendingPathComponent:@"Mobile Documents"];
查看 http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/c_ref/NSSearchPathDirectory 中的常量值, Mobile Documents
目录似乎没有特定常量,因此硬编码方法可能是您唯一的选择。
关于iphone - 移动文档文件夹的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13635301/