小编Kar*_*ste的帖子

Swift3.0 fileAttributes在现有文件上抛出"无此文件"错误

我是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)

file-attributes swift swift3

3
推荐指数
1
解决办法
593
查看次数

标签 统计

file-attributes ×1

swift ×1

swift3 ×1