iOS development-Data persistence for Swift & Universal file storage

Source: Internet
Author: User

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

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.