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?
NSHomeDirectory()
returns the root directory for your sandboxed app on iOS, but it's not recommended to use this.
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()
.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With