I want to understand the two commonly used, NSDocumentDirectory
which I have used in NSSearchPathForDirectoriesInDomains
. The other one is NSHomeDirectory
. What is the difference between these two?
我想了解我在NSSearchPathForDirectoriesInDomains中使用的两个常用的NSDocumentDirectory。另一个是NSHomeDirectory。这两者有什么区别?
1 个解决方案
#1
2
NSHomeDirectory()
returns the root directory for your sandboxed app on iOS, but it's not recommended to use this.
NSHomeDirectory()返回iOS上沙盒应用的根目录,但不建议使用它。
If you want to read and write files, the most common places to use are the documents directory and the caches directory. Use the former for things you want to save for a long period of time, and the latter for things you want to save only temporarily. Apple provides specific ways to find these two directories rather than trying to access them through the home directory, so you should use it: NSSearchPathForDirectoriesInDomains()
.
如果要读取和写入文件,最常用的位置是文档目录和caches目录。将前者用于您想要长时间保存的内容,将后者用于您想要暂时保存的内容。 Apple提供了查找这两个目录的具体方法,而不是尝试通过主目录访问它们,因此您应该使用它:NSSearchPathForDirectoriesInDomains()。
Lots of tutorials use NSHomeDirectory, and it wouldn't exactly be hard for Apple to remove the function from iOS if they really wanted it dead. However, in practice you should use the NSSearchPathForDirectoriesInDomains()
function to locate standard directories like Documents and Library/Caches – they could be moved anywhere at any time in the future, so it's sensible to ask iOS where they are rather than try to hard-code paths.
很多教程都使用了NSHomeDirectory,如果Apple真的想让它死掉,那么从iOS中删除这个功能并不是很难。但是,在实践中,您应该使用NSSearchPathForDirectoriesInDomains()函数来定位标准目录,例如Documents和Library / Caches - 它们可以在将来的任何时间移动到任何地方,因此向iOS询问它们的位置是合理的,而不是试图努力代码路径。
In summary, here's a comment I posted to the original question: "I'm curious you think it's common to use NSHomeDirectory() on iOS. It really isn't – you should request the path to the subfolder you want, e.g. documents or caches." The function exists, and many people use it, but I would strongly suggest you do not.
总之,这是我在原始问题上发表的评论:“我很好奇你认为在iOS上使用NSHomeDirectory()是很常见的。实际上并非如此 - 你应该请求你想要的子文件夹的路径,例如文件或高速缓存“。该功能存在,很多人都使用它,但我强烈建议你不要这样做。
#1
2
NSHomeDirectory()
returns the root directory for your sandboxed app on iOS, but it's not recommended to use this.
NSHomeDirectory()返回iOS上沙盒应用的根目录,但不建议使用它。
If you want to read and write files, the most common places to use are the documents directory and the caches directory. Use the former for things you want to save for a long period of time, and the latter for things you want to save only temporarily. Apple provides specific ways to find these two directories rather than trying to access them through the home directory, so you should use it: NSSearchPathForDirectoriesInDomains()
.
如果要读取和写入文件,最常用的位置是文档目录和caches目录。将前者用于您想要长时间保存的内容,将后者用于您想要暂时保存的内容。 Apple提供了查找这两个目录的具体方法,而不是尝试通过主目录访问它们,因此您应该使用它:NSSearchPathForDirectoriesInDomains()。
Lots of tutorials use NSHomeDirectory, and it wouldn't exactly be hard for Apple to remove the function from iOS if they really wanted it dead. However, in practice you should use the NSSearchPathForDirectoriesInDomains()
function to locate standard directories like Documents and Library/Caches – they could be moved anywhere at any time in the future, so it's sensible to ask iOS where they are rather than try to hard-code paths.
很多教程都使用了NSHomeDirectory,如果Apple真的想让它死掉,那么从iOS中删除这个功能并不是很难。但是,在实践中,您应该使用NSSearchPathForDirectoriesInDomains()函数来定位标准目录,例如Documents和Library / Caches - 它们可以在将来的任何时间移动到任何地方,因此向iOS询问它们的位置是合理的,而不是试图努力代码路径。
In summary, here's a comment I posted to the original question: "I'm curious you think it's common to use NSHomeDirectory() on iOS. It really isn't – you should request the path to the subfolder you want, e.g. documents or caches." The function exists, and many people use it, but I would strongly suggest you do not.
总之,这是我在原始问题上发表的评论:“我很好奇你认为在iOS上使用NSHomeDirectory()是很常见的。实际上并非如此 - 你应该请求你想要的子文件夹的路径,例如文件或高速缓存“。该功能存在,很多人都使用它,但我强烈建议你不要这样做。