我是Swift的新手,我正在尝试实现文件创建日期检查.
我们的想法是检查文件的创建时间是否超过7天.由于某种原因,代码总是返回"没有这样的文件"错误.
为了检查什么是错误,我使用相同的路径来读取同一功能中的文件内容,并且完美无缺.
我错误地使用了路径还是误解了某些东西?
func creationDateCheck(name: String) -> Bool {
// This is the path I am using, file is in the favorites folder in the .documentsDirectory
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let myFilesPath = documentsUrl.appendingPathComponent("favorites/" + name + ".xml")
let pathString = "\(myFilesPath)"
//First do block tries to get the file attributes and fails
do {
let fileAttributes = try FileManager.default.attributesOfItem(atPath: pathString)
print(fileAttributes)
let creationDate = (fileAttributes[FileAttributeKey.creationDate] as? NSDate)!
return daysBetweenDates(endDate: creationDate as Date) > 7
} …Run Code Online (Sandbox Code Playgroud)