API参考Foundation
NSFileManager
文件系统操作,检查路径、创建目录、读写文件等。
用于文件系统操作。通过 NSFileManager.defaultManager() 获取默认实例。常与 Application.documentPath、Application.tempPath 配合使用。
类成员 (Class members)
defaultManager
获取默认文件管理器。
static defaultManager(): NSFileManagerReturn Value:
NSFileManager: 默认单例。
实例成员 (Instance members)
路径与目录
fileExistsAtPath
检查文件或目录是否存在。
fileExistsAtPath(path: string): booleanParameters:
| Name | Type | Description |
|---|---|---|
path | string | 文件或目录路径。 |
isDirectoryAtPath
检查路径是否为目录。
isDirectoryAtPath(path: string): booleanParameters:
| Name | Type | Description |
|---|---|---|
path | string | 路径。 |
createDirectoryAtPathWithIntermediateDirectoriesAttributes
创建目录(支持创建中间目录)。
createDirectoryAtPathWithIntermediateDirectoriesAttributes(path: string, withIntermediateDirectories: boolean, attributes: NSDictionary | null): booleanParameters:
| Name | Type | Description |
|---|---|---|
path | string | 目录路径。 |
withIntermediateDirectories | boolean | 是否创建中间目录。 |
attributes | NSDictionary | null | 目录属性(默认传 null)。 |
createDirectoryAtPathAttributes (简易版)
创建目录(简易版)。
createDirectoryAtPathAttributes(path: string, attributes: NSDictionary | null): booleanParameters:
| Name | Type | Description |
|---|---|---|
path | string | 目录路径。 |
attributes | NSDictionary | null | 目录属性。 |
contentsOfDirectoryAtPath
获取目录内容(浅层遍历)。
contentsOfDirectoryAtPath(path: string): string[]Parameters:
| Name | Type | Description |
|---|---|---|
path | string | 目录路径。 |
Return Value:
string[]: 文件名/子目录名列表。
subpathsOfDirectoryAtPath
获取目录的所有子路径(深层遍历)。
subpathsOfDirectoryAtPath(path: string): string[]Parameters:
| Name | Type | Description |
|---|---|---|
path | string | 目录路径。 |
subpathsAtPath
获取路径下的所有子路径。
subpathsAtPath(path: string): string[]Parameters:
| Name | Type | Description |
|---|---|---|
path | string | 路径。 |
directoryContentsAtPath
获取目录内容(旧 API)。
directoryContentsAtPath(path: string): string[]Parameters:
| Name | Type | Description |
|---|---|---|
path | string | 目录路径。 |
读写与复制
contentsAtPath
读取文件内容。
contentsAtPath(path: string): NSDataParameters:
| Name | Type | Description |
|---|---|---|
path | string | 文件路径。 |
Return Value:
NSData: 文件数据对象。
copyItemAtPathToPath
复制文件或目录。
copyItemAtPathToPath(srcPath: string, toPath: string): booleanParameters:
| Name | Type | Description |
|---|---|---|
srcPath | string | 源路径。 |
toPath | string | 目标路径。 |
moveItemAtPathToPath
移动(重命名)文件或目录。
moveItemAtPathToPath(srcPath: string, toPath: string): booleanParameters:
| Name | Type | Description |
|---|---|---|
srcPath | string | 源路径。 |
toPath | string | 目标路径。 |
属性与权限
attributesOfItemAtPath
获取文件/目录属性。
attributesOfItemAtPath(path: string): NSDictionaryParameters:
| Name | Type | Description |
|---|---|---|
path | string | 路径。 |
attributesOfFileSystemForPath
获取文件系统属性(如磁盘空间)。
attributesOfFileSystemForPath(path: string): NSDictionaryParameters:
| Name | Type | Description |
|---|---|---|
path | string | 路径。 |
fileAttributesAtPathTraverseLink
获取文件属性(遍历符号链接选项)。
fileAttributesAtPathTraverseLink(path: string, traverseLink: boolean): NSDictionaryParameters:
| Name | Type | Description |
|---|---|---|
path | string | 路径。 |
traverseLink | boolean | 是否跟随符号链接。 |
isReadableFileAtPath
检查文件是否可读。
isReadableFileAtPath(path: string): booleanisWritableFileAtPath
检查文件是否可写。
isWritableFileAtPath(path: string): booleanisExecutableFileAtPath
检查文件是否可执行。
isExecutableFileAtPath(path: string): booleanisDeletableFileAtPath
检查文件是否可删除。
isDeletableFileAtPath(path: string): boolean