Ios_swift File System

Source: Internet
Author: User
Tags save file

    Func files () {//Apply root directory Let Dir = Nshomedirectory (); NSLog ("dir = \ (dir)")//Document directory Let Doc = Nssearchpathfordirectoriesindomains (. Documentdirectory,. Userdomainmask, True) [0] NSLog ("doc = \ (doc)")//library directory Let Library = Nssearchpathfordire Ctoriesindomains (. Librarydirectory,. Userdomainmask, True) [0] NSLog ("doc = \ (library)")//Determine if directory exists let manager = Nsfilemanager.def            Aultmanager () Let MyDir = "\ (DOC)/my" Let isexist = Manager.fileexistsatpath (MyDir) if (isexist) { NSLog ("Existing My Folder")}else{NSLog ("My folder does not exist! ")//Create folder do{try Manager.createdirectoryatpath (MyDir, withintermediatedirectories: True, Attributes:nil)}catch{}}//Determine if the file exists let Mytxt = "\ (DOC)/my.tx        T "let Isexistmytxt = Manager.fileexistsatpath (mytxt) if (isexistmytxt) {    NSLog ("Presence My.txt")}else{NSLog ("My.txt not Present")//Create file Manager.createfileatpath (my TXT, Contents:nil, Attributes:nil)}//user preferences let Userdefault = Nsuserdefaults.standarduser  Defaults () userdefault.setobject ("+", Forkey: "Time") userdefault.synchronize () Let time =                Userdefault.stringforkey ("Time") NSLog ("time = \ (time!)") Save file Let Arr:nsarray = ["One", "" "," ""] arr.writetofile (Mytxt, atomically:true) let Conten        t = Nsarray (contentsoffile:mytxt)! Print (content)//object Save stuplist = "\ (DOC)/stu.plist" Let Stu = Student (name: "Zhangsan", a GE:20) Nskeyedarchiver.archiverootobject (Stu, tofile:stuplist) let STU2 = Nskeyedunarchiver.unarch Iveobjectwithfile (stuplist) as? Student if STU2! = Nil {print ("name = \ (stu2!. Name), age = \ (stu2!. Age)}}

Ios_swift File System

Related Article

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: 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.