什么是文档目录(NSDocumentDirectory)?
有人可以向我解释一下 iOS 应用程序上的文档目录是什么以及何时使用它吗?
以下是我目前的看法:
对我来说,它似乎是一个中心文件夹,用户可以在其中存储应用程序所需的任何文件。
这与 Core Data 存储数据的位置不同吗?
似乎每个应用程序都有自己的文档目录。
我可以自由创建文档目录的子目录,例如文档目录/图像或文档目录/视频?
Can someone explain to me what the documents directory is on an iOS app and when to use it?
Here is what I believe at present:
To me, it seems to be a central folder where the user can store any files needed for the app.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的应用程序仅(在未越狱的设备上)在“沙盒”环境中运行。这意味着它只能访问其自身内容内的文件和目录。例如文档和库。
请参阅iOS 应用程序编程指南。
要访问应用程序沙箱的 Documents 目录,您可以使用以下内容:
则建议使用此方法。
iOS 8 及更高版本,如果您需要支持 iOS 7 或更早版本,
此 Documents > 目录允许您存储应用程序创建或可能需要的文件和子目录。
要访问应用沙箱的 Library 目录中的文件,请使用(代替上面的
path
):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.
See the iOS Application Programming Guide.
To access the Documents directory of your applications sandbox, you can use the following:
iOS 8 and newer, this is the recommended method
if you need to support iOS 7 or earlier
This Documents directory allows you to store files and subdirectories your app creates or may need.
To access files in the Library directory of your apps sandbox use (in place of
paths
above):这在 iOS 8 中发生了变化。请参阅以下技术说明:https://developer.apple.com /library/ios/technotes/tn2406/_index.html
苹果认可的方式(来自上面的链接)如下:
This has changed in iOS 8. See the following tech note: https://developer.apple.com/library/ios/technotes/tn2406/_index.html
The Apple sanctioned way (from the link above) is as follows:
我在接受的答案建议的文档中找不到代码,但我在这里找到了更新的等效代码:
文件系统编程指南 :: 访问文件和目录 »
它不鼓励使用NSSearchPathForDirectoriesInDomain:
这里有一些其他有用的目录常量。毫无疑问,iOS 并不支持所有这些。您还可以使用 NSHomeDirectory() 函数:
来自 NSPathUtilities.h
最后是 NSURL 类别中的一些便捷方法
http://club15cc.com /code/ios/easy-ios-file-directory-paths-with-this-handy-nsurl-category
I couldn't find the code in the doc suggested by the accepted answer but I found the updated equivalent here:
File System Programming Guide :: Accessing Files and Directories »
It discourages use of NSSearchPathForDirectoriesInDomain:
Here are some other useful directory constants to play with. No doubt not all of these are supported in iOS. Also you can use the NSHomeDirectory() function which:
From NSPathUtilities.h
And finally, some convenience methods in an NSURL category
http://club15cc.com/code/ios/easy-ios-file-directory-paths-with-this-handy-nsurl-category
Swift 3 和 4 作为全局变量:
作为 FileManager 扩展:
Swift 3 and 4 as global var:
As FileManager extension:
除了
Documents
文件夹之外,iOS 还允许您将文件保存到temp
和Library
文件夹。有关使用哪一个的更多信息,请参阅文档中的以下链接:
Aside from the
Documents
folder, iOS also lets you save files to thetemp
andLibrary
folders.For more information on which one to use, see this link from the documentation:
为这种尴尬的调用添加一个扩展到 FileManager 会更干净,如果没有别的办法的话,也是为了整洁。像这样的东西:
It can be cleaner to add an extension to FileManager for this kind of awkward call, for tidiness if nothing else. Something like:
您可以使用此代码访问文档目录,它主要用于存储 plist 格式的文件:
You can access documents directory using this code it is basically used for storing file in plist format:
这是一个有用的小功能,它使使用/创建 iOS 文件夹变得更加容易。
您将子文件夹的名称传递给它,它会将完整路径返回给您,并确保该目录存在。
(就我个人而言,我将此静态函数放在我的 AppDelete 类中,但也许这不是放置它的最明智的地方。)
是您如何调用它,以获取 MySavedImages 子目录的“完整路径”:
以下 函数:
使用这个小函数,可以轻松地在应用程序的 Documents 目录中创建一个目录(如果它尚不存在),并向其中写入文件。
以下是我创建目录并将图像文件之一的内容写入其中的方法:
希望这有帮助!
Here's a useful little function, which makes using/creating iOS folders a little easier.
You pass it the name of a subfolder, it'll return the full path back to you, and make sure the directory exists.
(Personally, I stick this static function in my AppDelete class, but perhaps this isn't the smartest place to put it.)
Here's how you would call it, to get the "full path" of a MySavedImages subdirectory:
And here's the full function:
Using this little function, it's easy to create a directory in your app's Documents directory (if it doesn't already exist), and to write a file into it.
Here's how I would create the directory, and write the contents of one of my image files into it:
Hope this helps !
与其他人提到的一样,您的应用程序在沙盒环境中运行,您可以使用文档目录来存储应用程序可能使用的图像或其他资产,例如。根据用户喜好下载离线文件 - 文件系统基础 - Apple 文档 - 使用哪个目录,用于存储特定于应用程序的目录files
更新到 swift 5,您可以根据要求使用这些函数之一 -
用法:
Like others mentioned, your app runs in a sandboxed environment and you can use the documents directory to store images or other assets your app may use, eg. downloading offline-d files as user prefers - File System Basics - Apple Documentation - Which directory to use, for storing application specific files
Updated to swift 5, you can use one of these functions, as per requirement -
Usage: