我有创建导演的功能:
func createSystemFolders(){
// Create a FileManager instance
let fileManager = FileManager.default
do {
try fileManager.createDirectory(atPath: "json", withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError {
debugPrint("\(ErrorsLabels.AppDelegate01): \(error)")
}
do {
try fileManager.createDirectory(atPath: "inspirations", withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError {
debugPrint("\(ErrorsLabels.AppDelegate02): \(error)")
}
do {
try fileManager.createDirectory(atPath: "products", withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError {
debugPrint("\(ErrorsLabels.AppDelegate03): \(error)")
}
}
我需要第二个功能来检查目录是否存在。
我可以检查一下吗?答案 0 :(得分:2)
你可以使用它,
fileprivate func directoryExistsAtPath(_ path: String) -> Bool {
var isdirectory : ObjCBool = true
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return exists && isDirectory.boolValue
}
答案 1 :(得分:1)
你可以做更聪明的解决方案就像没有两个功能的completion(isExit,directoryURL)
:
简单地在一行中使用:
self.createSystemFolders("json") { (isExit, url) in
print(isExit)
print(url)
}
CreateSystemFolders:
func createSystemFolders(_ folderName:String ,_ completion:(_ isExit:Bool?,_ directoryURL:URL?) -> Void){
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
let directory = paths[0]
let fileManager = FileManager.default
let url = URL(fileURLWithPath: directory).appendingPathComponent(folderName)
if !fileManager.fileExists(atPath: url.path) {
do {
try fileManager.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
completion(false,url)
}
catch {
print("Error: Unable to create directory: \(error)")
completion(nil,nil)
}
var url = URL(fileURLWithPath: directory)
var values = URLResourceValues()
values.isExcludedFromBackup = true
do {
try url.setResourceValues(values)
completion(false,url)
}
catch {
print("Error: Unable to exclude directory from backup: \(error)")
completion(nil,nil)
}
}else{
completion(true,url)
}
}
答案 2 :(得分:0)
试试这个:
let fileManager = FileManager.default
var isdirectory : ObjCBool = true
if fileManager.fileExists(atPath: fullPath, isDirectory:&isdirectory) {
if isdirectory.boolValue {
// file exists and is a directory
} else {
// file exists and is not a directory
}
} else {
// file does not exist
}
答案 3 :(得分:0)
这里你怎么可以
1在设备上找到文档目录
2检查文件是否存在于指定的文件路径
let fileNameToDelete = "myFileName.txt"
var filePath = ""
// Fine documents directory on device
let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0 {
let dir = dirs[0] //documents directory
filePath = dir.appendingFormat("/" + fileNameToDelete)
print("Local path = \(filePath)")
} else {
print("Could not find local directory to store file")
return
}
let fileManager = FileManager.default
// Check if file exists
if fileManager.fileExists(atPath: filePath) {
print("File exists")
} else {
print("File does not exist")
}