Until now, I've been using the following to initialize my NSPersistentStoreCoordinator's db path:

[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject];

However, Xcode now auto-populates my app delegate with a method

(NSURL *)applicationDocumentsDirectory:

which returns:

[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

Are there benefits or drawbacks to either one or are they just two ways of doing the exact same thing?

有帮助吗?

解决方案

Using NSFileManager is the preferred way as written in the Docs: Foundation Functions Reference (NSSearchPathForDirectoriesInDomains)

You should consider using the NSFileManager methods URLsForDirectory:inDomains: and URLForDirectory:inDomain:appropriateForURL:create:error:. which return URLs, which are the preferred format.

I don't know if there are any real drawbacks when using NSSearchPathForDirectoriesInDomains to determine the URL to your documents directory. I would recommend to use NSFileManager and it's method as this is the quasi-convenient way.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top