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

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

    Very simple: If your path is a URL instance convert to string by 'path' method.

        let fileManager = FileManager.default
        var isDir: ObjCBool = false
        if fileManager.fileExists(atPath: yourURLPath.path, isDirectory: &isDir) {
            if isDir.boolValue {
                //it's a Directory path
            }else{
                //it's a File path
            }
        }
    

提交回复
热议问题