Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何获取用户';macOS中的特殊文件夹是什么?_Objective C_Swift_Macos - Fatal编程技术网

Objective c 如何获取用户';macOS中的特殊文件夹是什么?

Objective c 如何获取用户';macOS中的特殊文件夹是什么?,objective-c,swift,macos,Objective C,Swift,Macos,是否有任何API可用于获取用户的特殊文件夹,如macOS中的下载或文档文件夹?根据我的经验,我通常用于获取主目录路径,如下例所示 第一路: NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES); NSString * desktopPath = [paths objectAtIndex:0]; [NSURL fileURLWithPath:NSHomeD

是否有任何API可用于获取用户的特殊文件夹,如macOS中的下载或文档文件夹?

根据我的经验,我通常用于获取主目录路径,如下例所示

第一路:

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString * desktopPath = [paths objectAtIndex:0];
[NSURL fileURLWithPath:NSHomeDirectory()];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]];
对于桌面:
NSDesktopDirectory

对于文档:
NSDocumentDirectory

下载:
NSDownloadsDirectory

第二种方式:

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString * desktopPath = [paths objectAtIndex:0];
[NSURL fileURLWithPath:NSHomeDirectory()];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]];
这不是最好的方法,但我们也可以通过这种方法得到路径

对不同的语言使用本地化

$当前用户的主目录:

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString * desktopPath = [paths objectAtIndex:0];
[NSURL fileURLWithPath:NSHomeDirectory()];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]];
$Home/Desktop

[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"]];
$Home/Documents:

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString * desktopPath = [paths objectAtIndex:0];
[NSURL fileURLWithPath:NSHomeDirectory()];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]];
$Home/下载:

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString * desktopPath = [paths objectAtIndex:0];
[NSURL fileURLWithPath:NSHomeDirectory()];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]];

希望这将帮助您找到特殊的文件夹路径。

正如其他人已经提到的,更现代的方法是使用
NSFileManager
检索
URL

NSURL *desktopUrl = [NSFileManager.defaultManager URLsForDirectory:NSDesktopDirectory inDomains:NSUserDomainMask].firstObject;
Swift 5.x桌面:

var DesktopDir : String = {
    let paths = NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true)
    return paths[0]
}()

使用NSFileManager defaultManager更适合查找桌面、文档等。无需附加字符串组件。。。。无论应用程序是否为沙盒,它都能工作,为相应的配置返回适当的路径。“stringByAppending”不能保证工作。意大利用户可能没有名为Desktop的文件夹,对于任何用户,这些文件夹可能会在未来的操作系统版本中更改其位置。我不建议使用字符串,但为了清楚起见,文件夹名称不会以这种方式本地化。意大利人或任何其他本地化用户也会有相同的文件夹名称,它只会在Finder中显示不同的名称。谢谢。这有助于我找到正确的道路。如期工作