您好,登錄后才能下訂單哦!
NSFileManager是iOS和macOS中用于管理文件系統(tǒng)的類,可以用來創(chuàng)建、復(fù)制、移動(dòng)、刪除文件和目錄,以及獲取文件和目錄的屬性等操作。下面是一些常見的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的用法,根據(jù)實(shí)際需求可以進(jìn)一步擴(kuò)展和應(yīng)用。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。