Universal file Storage
1 Import UIKit2 3 classViewcontroller:uiviewcontroller {4 5@IBOutlet weak var textfield:uitextfield!6 7 @IBAction func btnsave (Sender:uibutton) {8var text = Textfield.text asNSString9 Ten //write to file (serializable) OneText.writetofile (GetPath ("Data.txt"), atomically:false, encoding:nsutf8stringencoding, Error:nil) A - } - the @IBAction func btnLoad (Sender:uibutton) { - //get a TXT file directory -var Txtfilepath = GetPath ("Data.txt")//String Data - ifNsfilemanager.defaultmanager (). Fileexistsatpath (Txtfilepath) { +var text =NSString (Contentsoffile:txtfilepath, encoding:nsutf8stringencoding, Error:nil) -Textfield.text = text as!String + } A at } - - Overridefunc viewdidload () { - super.viewdidload () - /** - Generic File Store classes (dictionaries and arrays) that can be serialized in NSData - nsstring (String) to NSNumber (Int, Double, Float) + NSDate - Nsarray (Array) the nsdictionary * */ $ /** Objects created by themselves cannot bePanax Notoginseng no other class that can serialize an object in front of it (uicolor,uiimage) - not in itself . the Big Data Half not + */ A the + } - $ Overridefunc didreceivememorywarning () { $ super.didreceivememorywarning () - //Dispose of any resources the can be recreated. - } the //Get file directory -Func GetPath (filename:string)String {Wuyivar paths = Nssearchpathfordirectoriesindomains (Nssearchpathdirectory.documentdirectory, Nssearchpathdomainmask.userdomainmask,true) thevar documents = paths[0] as?String - returndocuments!. stringByAppendingPathComponent (fileName) Wu } - About}
iOS development-Data persistence for Swift & Universal file storage