您好,登錄后才能下訂單哦!
NSFileManager是iOS和macOS中用于管理文件系統的類,可以用來創建、復制、移動、刪除文件和目錄,以及獲取文件和目錄的屬性等操作。下面是一些常見的NSFileManager使用方法:
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let folderURL = documentsURL.appendingPathComponent("FolderName")
do {
try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error creating folder: \(error)")
}
let sourceURL = URL(fileURLWithPath: "path/to/source/file")
let destinationURL = URL(fileURLWithPath: "path/to/destination/file")
do {
try fileManager.copyItem(at: sourceURL, to: destinationURL)
} catch {
print("Error copying file: \(error)")
}
let sourceURL = URL(fileURLWithPath: "path/to/source/file")
let destinationURL = URL(fileURLWithPath: "path/to/destination/file")
do {
try fileManager.moveItem(at: sourceURL, to: destinationURL)
} catch {
print("Error moving file: \(error)")
}
let fileURL = URL(fileURLWithPath: "path/to/file")
do {
try fileManager.removeItem(at: fileURL)
} catch {
print("Error deleting file: \(error)")
}
let fileURL = URL(fileURLWithPath: "path/to/file")
do {
let attributes = try fileManager.attributesOfItem(atPath: fileURL.path)
print(attributes)
} catch {
print("Error getting file attributes: \(error)")
}
這些是一些基本的NSFileManager的用法,根據實際需求可以進一步擴展和應用。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。