Get the Documents Path first
Class Viewcontroller:uiviewcontroller { //MARK:-Properties lazy var documentspath:string = {Let paths = Nssearchpathfordirectoriesindomains (. Documentdirectory,. Userdomainmask, True) return paths.first! } ()}
The first implementation of the directory query, create and delete it,?? Check if directory exists??
Func Directoryexistsatpath (path:string), Bool {let FileManager = Nsfilemanager.defaultmanager () Let result = Filemanager.fileexistsatpath (path) If result { print ("directory exists ...") } else { print ( "Directory Not exists ...") } return result }
?? Create a directory??
Func Createdirectoryatpath (path:string) {let FileManager = Nsfilemanager.defaultmanager () do { try Filemanager.createdirectoryatpath (Path, withintermediatedirectories:false, Attributes:nil)//path: directory path, Withintermediatedirectories: Tells the system that the parent directory exists, true indicates the need to create a multilevel directory, attributes: folder-related properties } catch { print ("Create Directory failed ... ") } }
?? Delete directory??
Func Deletedirectoryatpath (path:string) {let FileManager = Nsfilemanager.defaultmanager () do { try Filemanager.removeitematpath (path) } catch { print ("Delete directory failed ...") } }
Take a look at the file related operations, in fact, and the directory is similar
?? Check the file??
Func filetest () { ///~/documents/data.txt// Check if file exists, if not present, create file let path = "\ (documentspath)/data.txt" print (path) if!fileexistsatpath (path) { Createfileatpath (path) Fileexistsatpath (path) } //delete directory Deletefileatpath (path) //delete, and then check fileexistsatpath (path) } func Fileexistsatpath (path:string), Bool {let FileManager = Nsfilemanager.defaultmanager () let result = Filemanager.fileexistsatpath (path) If result { print ("file exists ...") } else { print ("File not Exists ... ") } return result }
?? Create a file??
Func Createfileatpath (path:string) {let FileManager = Nsfilemanager.defaultmanager () Filemanager.createfileatpath (Path, Contents:nil, Attributes:nil)//path represents the file path, and contents represents the content that you want to write to the file. Attributes representing file-related properties }
?? Delete file??
Func Deletefileatpath (path:string) {let FileManager = Nsfilemanager.defaultmanager () do { try Filemanager.removeitematpath (path) } catch { print ("Delete directory failed ...") } }
It is easy to compare the different points in the creation and deletion of the method, the creation of the directory is
. Createdirectoryatpath
Create File is
. Createfileatpath
Delete, uh, delete are all
. removeItemAtPath
Directory file management for iOS development