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 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 – 如何查找重要的用户目录?全部内容,希望文章能够帮你解决Swift 4 macOS App – 如何查找重要的用户目录?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)