How to check if a file exists in the Documents directory in Swift?

前端 未结 12 2126
梦毁少年i
梦毁少年i 2020-11-28 21:19

How to check if a file exists in the Documents directory in Swift?

I am using [ .writeFilePath ] method to save an image into the Documents

12条回答
  •  北海茫月
    2020-11-28 21:39

    works at Swift 5

        do {
            let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            let fileUrl = documentDirectory.appendingPathComponent("userInfo").appendingPathExtension("sqlite3")
            if FileManager.default.fileExists(atPath: fileUrl.path) {
                print("FILE AVAILABLE")
            } else {
                print("FILE NOT AVAILABLE")
            }
        } catch {
            print(error)
        }
    

    where "userInfo" - file's name, and "sqlite3" - file's extension

提交回复
热议问题