- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
对于 macOS 应用程序,使用 Swift 4 查找重要用户目录(如桌面、文档、下载等)的最简单方法是什么?
最佳答案
简单描述:
let desktopDir = try? FileManager.default.url(for: .desktopDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let documentDir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let downloadsDir = try? FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
有两种方法可以从 FileManager
类中获取此信息:
func urls(for directory: FileManager.SearchPathDirectory,
in domainMask: FileManager.SearchPathDomainMask) -> [URL]
Returns an array of URLs for the specified common directory in the requested domains. […] 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.
文档:https://developer.apple.com/documentation/foundation/filemanager/1407726-urls
(在@leo-dabus 的回答中已经提到)
(我更喜欢它,因为它更通用并且返回单个 URL)
func url(for directory: FileManager.SearchPathDirectory,
in domain: FileManager.SearchPathDomainMask,
appropriateFor url: URL?,
create shouldCreate: Bool) throws -> URL
Locates and optionally creates the specified common directory in a domain.
文档:https://developer.apple.com/documentation/foundation/filemanager/1407693-url
https://developer.apple.com/documentation/foundation/filemanager.searchpathdomainmask 中定义的有效域是:
.userDomainMask
: The user’s home directory—the place to install user’s personal items (~).
.localDomainMask
: Local to the current machine—the place to install items available to everyone on this machine.
.networkDomainMask
: Publicly available location in the local area network—the place to install items available on the network (/Network).
.systemDomainMask
: Provided by Apple—can’t be modified (/System) .
.allDomainsMask
: All domains.
在 https://developer.apple.com/documentation/foundation/filemanager.searchpathdirectory 中定义的 directory
参数有很多可能性:
.applicationDirectory
: Supported applications (/Applications).
.demoApplicationDirectory
: Unsupported applications and demonstration versions.
.developerApplicationDirectory
: Developer applications (/Developer/Applications).
.adminApplicationDirectory
System and network administration applications.
.libraryDirectory
: Various user-visible documentation, support, and configuration files (/Library).
.developerDirectory
: Developer resources (/Developer).
.userDirectory
: User home directories (/Users).
.documentationDirectory
: Documentation.
.documentDirectory
: Document directory.
.coreServiceDirectory
: Location of core services (System/Library/CoreServices).
.autosavedInformationDirectory
: Location of user’s autosaved documents (Library/Autosave Information).
.desktopDirectory
: Location of user’s desktop directory.
.cachesDirectory
: Location of discardable cache files (Library/Caches).
.applicationSupportDirectory
: Location of application support files (Library/Application Support).
.downloadsDirectory
: Location of the user’s downloads directory.
.inputMethodsDirectory
: Location of Input Methods (Library/Input Methods).
.moviesDirectory
: Location of user's Movies directory (~/Movies).
.musicDirectory
: Location of user's Music directory (~/Music).
.picturesDirectory
: Location of user's Pictures directory (~/Pictures).
.printerDescriptionDirectory
: Location of system's PPDs directory (Library/Printers/PPDs).
.sharedPublicDirectory
: Location of user's Public sharing directory (~/Public).
.preferencePanesDirectory
: Location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes).
.applicationScriptsDirectory
: Location of the user scripts folder for the calling application (~/Library/Application Scripts/),
.itemReplacementDirectory
: Passed to the FileManager methodurl(for:in:appropriateFor:create:)
in order to create a temporary directory.
.allApplicationsDirectory
: All directories where applications can occur.
.allLibrariesDirectory
: All directories where resources can occur.
.trashDirectory
: Location of the trash directory.
关于Swift 4 macOS App - 如何找到重要的用户目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46997820/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!