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

前端 未结 12 2135
梦毁少年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:56

    Swift 4.2

    extension URL    {
        func checkFileExist() -> Bool {
            let path = self.path
            if (FileManager.default.fileExists(atPath: path))   {
                print("FILE AVAILABLE")
                return true
            }else        {
                print("FILE NOT AVAILABLE")
                return false;
            }
        }
    }
    

    Using: -

    if fileUrl.checkFileExist()
       {
          // Do Something
       }
    

提交回复
热议问题