gpt4 book ai didi

swift - fileExistsAtPath 检查文件名?

转载 作者:行者123 更新时间:2023-11-28 16:00:25 24 4
gpt4 key购买 nike

如何判断目录下是否有只有名字没有扩展名的文件?现在文件已写入我的目录,它们的名称将从 id 文件生成。因此,当我在查找文件时,在它所在的目录中让 file = "\(fileId) .pdf",但如果没有扩展名,将找不到。从服务器返回更容易的扩展?

public var  isDownloaded: Bool {
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.URLByAppendingPathComponent("\(fileMessageModel.attachment.id)")!.path!
let fileManager = NSFileManager.defaultManager()
return fileManager.fileExistsAtPath(filePath)
}

最佳答案

enumeratorAtPath 创建一个深度枚举器——也就是说,它将扫描子文件夹及其子文件夹的内容。对于浅搜索,用户 contentOfDirectortAtPath:

func file(fileName: String, existsAt path: String) -> Bool {
var isFound = false

if let pathContents = try? NSFileManager.defaultManager().contentsOfDirectoryAtPath(path) {
pathContents.forEach { file in
if (file as NSString).stringByDeletingPathExtension == fileName {
isFound = true
return
}
}
}
return isFound
}

if let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
if file("something", existsAt: path) {
// The file exists, do something about it
}
}

关于swift - fileExistsAtPath 检查文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com