Directory file management for iOS development

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.