在iPhone上找到用户文档目录的最好方法是什么?

时间:2022-07-29 20:06:37

I'm reading Erica Sadun's iPhone Developer's Cookbook, and ran into a question.

我正在读Erica Sadun的iPhone开发者的食谱,遇到了一个问题。

She says in the book that the way to find the user's Documents directory is with the code:

她在书中说,找到用户文档目录的方法是使用代码:

[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

but that seems slightly brittle, and dissimiliar to the normal Mac way of doing it, which would be:

但这似乎有点脆弱,与普通的Mac方式不同,它是:

NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);

Are there any particular reasons to use one over the other?

有什么特别的理由来使用其中一个吗?

5 个解决方案

#1


89  

Objc:

Objc:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

Swift:

迅速:

var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

You'll want the first element of the returned array.

您将需要返回数组的第一个元素。

#2


48  

Here is the code that I use in my framework.

这是我在框架中使用的代码。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

#3


14  

You should consider using the NSFileManager methods which return URLs, which are the preferred format.

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL

This method is intended to locate known and common directories in the system.

该方法旨在定位系统中已知的和公共的目录。

An array of NSURL objects identifying the requested directories. The directories are ordered according to the order of the domain mask constants, with items in the user domain first and items in the system domain last.

标识所请求目录的NSURL对象数组。目录根据域掩码常量的顺序进行排序,首先是用户域中的项,最后是系统域中的项。

#4


0  

I use this

我用这个

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *zipLocalPath = [documentPath stringByAppendingString:fileName];

#5


0  

In swift v3, I used the following snippet

在swift v3中,我使用了以下代码片段

var paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)

#1


89  

Objc:

Objc:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

Swift:

迅速:

var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

You'll want the first element of the returned array.

您将需要返回数组的第一个元素。

#2


48  

Here is the code that I use in my framework.

这是我在框架中使用的代码。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

#3


14  

You should consider using the NSFileManager methods which return URLs, which are the preferred format.

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL

This method is intended to locate known and common directories in the system.

该方法旨在定位系统中已知的和公共的目录。

An array of NSURL objects identifying the requested directories. The directories are ordered according to the order of the domain mask constants, with items in the user domain first and items in the system domain last.

标识所请求目录的NSURL对象数组。目录根据域掩码常量的顺序进行排序,首先是用户域中的项,最后是系统域中的项。

#4


0  

I use this

我用这个

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *zipLocalPath = [documentPath stringByAppendingString:fileName];

#5


0  

In swift v3, I used the following snippet

在swift v3中,我使用了以下代码片段

var paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)