macos 获取当前用户的家目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6958448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current user's home directory
提问by sashoalm
I'm developing a Cocoa Objective-C application that will run on Mac OS X. I need to get the full path of the current user's home directory:
我正在开发一个将在 Mac OS X 上运行的 Cocoa Objective-C 应用程序。我需要获取当前用户主目录的完整路径:
/Users/MyUser/
Is there a standard function that does that in Objective-C/Cocoa?
在 Objective-C/Cocoa 中是否有一个标准函数可以做到这一点?
回答by zoul
NSHomeDirectory
: “Returns the path to the current user's home directory.”
NSHomeDirectory
: “返回当前用户主目录的路径。”
Example:
例子:
NSLog(@"Current user's home directory is %@", NSHomeDirectory());
回答by just_a_guy
Since macOS 10.12 you can also use FileManager.default.homeDirectoryForCurrentUser
, see the documentation.
从 macOS 10.12 开始,您也可以使用FileManager.default.homeDirectoryForCurrentUser
,请参阅文档。