加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 4 macOS App – 如何查找重要的用户目录?

发布时间:2020-12-14 04:36:18 所属栏目:百科 来源:网络整理
导读:使用 Swift 4为macOS应用程序找到重要用户目录的最简单方法是:桌面,文档,下载等? 解决方法 TLDR: let desktopDir = try? FileManager.default.url(for: .desktopDirectory,in: .userDomainMask,appropriateFor: nil,create: false)let documentDir = try?
使用 Swift 4为macOS应用程序找到重要用户目录的最简单方法是:桌面,文档,下载等?

解决方法

TLDR:

let desktopDir = try? FileManager.default.url(for: .desktopDirectory,in: .userDomainMask,appropriateFor: nil,create: false)
let documentDir = try? FileManager.default.url(for: .documentDirectory,create: false)
let downloadsDir = try? FileManager.default.url(for: .downloadsDirectory,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中定义:

.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 method
url(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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读