什么是文档目录(NSDocumentDirectory)? [英] What is the documents directory (NSDocumentDirectory)?

查看:29
本文介绍了什么是文档目录(NSDocumentDirectory)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释一下 iOS 应用上的文档目录是什么以及何时使用它吗?

Can someone explain to me what the documents directory is on an iOS app and when to use it?

以下是我目前的看法:

对我来说,它似乎是一个中央文件夹,用户可以在其中存储应用程序所需的任何文件.

To me, it seems to be a central folder where the user can store any files needed for the app.

这会与 Core Data 存储数据的位置不同吗?

This would be a different location than where Core Data stores its data?

似乎每个应用都有自己的文档目录.

It seems like each app gets its own documents directory.

我可以随意创建文档目录的子目录,例如文档目录/图像或文档目录/视频?

I am free to create a subdirectory of the documents directory, like documents directory/images, or documents directory/videos?

推荐答案

您的应用程序仅(在非越狱设备上)在沙盒"环境中运行.这意味着它只能访问自己内容中的文件和目录.例如文档图书馆.

Your app only (on a non-jailbroken device) runs in a "sandboxed" environment. This means that it can only access files and directories within its own contents. For example Documents and Library.

请参阅 iOS 应用程序编程指南.

要访问应用程序沙箱的文档目录,您可以使用以下命令:

To access the Documents directory of your applications sandbox, you can use the following:

+ (NSURL *)applicationDocumentsDirectory
{
     return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

如果您需要支持 iOS 7 或更早版本

+ (NSString *) applicationDocumentsDirectory 
{    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = paths.firstObject;
    return basePath;
}

Documents 目录允许您存储应用创建或可能需要的文件和子目录.

This Documents directory allows you to store files and subdirectories your app creates or may need.

要访问应用沙盒使用的目录中的文件(代替上面的paths):

To access files in the Library directory of your apps sandbox use (in place of paths above):

[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]

这篇关于什么是文档目录(NSDocumentDirectory)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆