Class
FileManager
A convenient interface to the contents of the file system, and the primary means of interacting with it.
--便于与文件系统内容进行交互的接口,以及与之交互的主要方式。
--里面有一些方法是macOS才可以使用的,有一些方法是IOS才可以使用的,但是文档并没有做说明,所以遇到找不到这些方法的时候,自行百度是啥系统下使用的吧。
Declaration --声明
class FileManager : NSObject
Overview --概览
A file manager object lets you examine the contents of the file system and make changes to it. The FileManager
class provides convenient access to a shared file manager object that is suitable for most types of file-related manipulations. A file manager object is typically your primary mode of interaction with the file system. You use it to locate, create, copy, and move files and directories. You also use it to get information about a file or directory or change some of its attributes.
--文件管理器对象允许您检查文件系统的内容并对其进行更改。FileManager类提供了对共享文件管理器对象的进行访问的便捷接口,该对象适用于大多数类型的文件相关操作。文件管理器对象通常是与文件系统进行交互的主要模式。您可以使用它定位、创建、复制和移动文件和目录。您还可以使用它来获取关于文件或目录的信息,或更改其某些属性。
When specifying the location of files, you can use either NSURL
or NSString
objects. The use of the NSURL
class is generally preferred for specifying file-system items because URLs can convert path information to a more efficient representation internally. You can also obtain a bookmark from an NSURL
object, which is similar to an alias and offers a more sure way of locating the file or directory later.
--当指定文件的位置时,可以使用NSURL或NSString对象。对于指定文件系统项,通常首选使用NSURL类,因为url可以在内部将路径信息转换为更有效的表示。您还可以从NSURL对象获得书签,它类似于别名,并提供了一种更确定的方法来稍后定位文件或目录。
If you are moving, copying, linking, or removing files or directories, you can use a delegate in conjunction with a file manager object to manage those operations. The delegate’s role is to affirm the operation and to decide whether to proceed when errors occur. In macOS 10.7 and later, the delegate must conform to the FileManagerDelegate
protocol.
--如果要移动、复制、链接或删除文件或目录,则可以将委托与文件管理器对象结合使用来管理这些操作。委托的角色是确认操作并在出现错误时决策是否继续执行。在macOS 10.7及以后版本中,委托必须遵守FileManagerDelegate协议。
In iOS 5.0 and later and in macOS 10.7 and later, FileManager
includes methods for managing items stored in iCloud. Files and directories tagged for cloud storage are synced to iCloud so that they can be made available to the user’s iOS devices and Macintosh computers. Changes to an item in one location are propagated to all other locations to ensure the items stay in sync.
--在ios5.0和更高版本以及macOS 10.7和更高版本中,FileManager包含了管理存储在iCloud中的条目的方法。标记为云存储的文件和目录会同步到iCloud,这样用户的iOS设备和Macintosh电脑就可以使用它们。对某个位置项的更改将传播到所有其他位置,以确保项保持同步。
Threading Considerations --多线程事项
The methods of the shared FileManager
object can be called from multiple threads safely. However, if you use a delegate to receive notifications about the status of move, copy, remove, and link operations, you should create a unique instance of the file manager object, assign your delegate to that object, and use that file manager to initiate your operations.
--可以从多个线程安全地调用共享FileManager对象的方法。但是,如果使用委托来接收关于移动、复制、删除和链接操作状态的通知,则应该创建文件管理器对象的唯一实例,将委托分配给该对象,并使用该文件管理器来初始化操作。
Topics --专题
Creating a File Manager --创建一个文件管理器
init(authorization: NSWorkspace.Authorization)
Initializes a file manager object that is authorized to perform privileged file system operations.
--初始化被授权执行特权文件系统的操作的文件管理器对象。
class var `default`: FileManager
The shared file manager object for the process.
--进程的共享文件管理器对象。
Accessing User Directories --访问用户目录
var homeDirectoryForCurrentUser: URL
The home directory for the current user.
--当前用户的主目录。是当前app的根目录
func NSHomeDirectory() -> String
Returns the path to either the user’s or application’s home directory, depending on the platform.
Returns the logon name of the current user.
func NSFullUserName() -> String
Returns a string containing the full name of the current user.
func homeDirectory(forUser: String) -> URL?
Returns the home directory for the specified user.
func NSHomeDirectoryForUser(String?) -> String?
Returns the path to a given user’s home directory.
The temporary directory for the current user.
--根目录下的temp目录
func NSTemporaryDirectory() -> String
Returns the path of the temporary directory for the current user.