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