简体   繁体   English

获取当前用户的主目录

[英]Get current user's home directory

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 中是否有一个标准函数可以做到这一点?

NSHomeDirectory : “Returns the path to the current user's home directory.” NSHomeDirectory :“返回当前用户主目录的路径。”

Example:例子:

NSLog(@"Current user’s home directory is %@", NSHomeDirectory());

从 macOS 10.12 开始,您还可以使用FileManager.default.homeDirectoryForCurrentUser ,请参阅文档

You can use FileManager.default.homeDirectoryForCurrentUser -> URL or NSHomeDirectory() -> String - they both return the same thing, just in different types.您可以使用FileManager.default.homeDirectoryForCurrentUser -> URLNSHomeDirectory() -> String - 它们都返回相同的内容,只是类型不同。

There's an important caveat, though:不过,有一个重要的警告:

In macOS, it is the application's sandbox directory or the current user's home directory (if the application is not in a sandbox).在 macOS 中,它是应用程序的沙箱目录或当前用户的主目录(如果应用程序不在沙箱中)。

If you're building your app to be distributed in the App Store, you're going to have to deal with the fact that these functions return the sandboxed home and not the directory the user thinks of as ~ .如果您正在构建要在 App Store 中分发的应用程序,您将不得不处理这样一个事实,即这些函数返回沙盒主目录而不是用户认为的目录~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM