iOS development-Data Persistence Swift & model object archiving

Source: Internet
Author: User

Model Object Archiving

1 Import UIKit2 3 classViewcontroller:uiviewcontroller {4 5     6     7@IBOutlet weak var textfield:uitextfield!8     9 @IBAction func btnsave (Sender:uibutton) {Tenvar text = Textfield.text asNSString Onevar image = UIImage (named:"Log") A          -var Textfilepath = GetPath ("Data.txt") -var person =Person () thePerson.name =text -Person.log =Image -          -var data =nskeyedarchiver.archiveddatawithrootobject (person) +Data.writetofile (Textfilepath, atomically:true) -     } +      A @IBAction func btnLoad (Sender:uibutton) { atvar Textfilepath = GetPath ("Data.txt") -var person = nskeyedunarchiver.unarchiveobjectwithfile (Textfilepath) as Person -Textfield.text =Person.name -Textfield.image =Person.image -          - println (log.image) in println (person.name) -     } to      +      -@IBOutlet weak var logimage:uiimageview! the      *     Overridefunc viewdidload () { $ super.viewdidload ()Panax Notoginseng          -         /** the Archive +         */ A          the          +         /** - unarchive $         */ $          -          -          the     } - Wuyi     Overridefunc didreceivememorywarning () { the super.didreceivememorywarning () -         //Dispose of any resources the can be recreated. Wu     } -  About  $     //Get file directory -Func GetPath (filename:string)String { -var paths = Nssearchpathfordirectoriesindomains (Nssearchpathdirectory.documentdirectory, Nssearchpathdomainmask.userdomainmask,true) -var documents = paths[0] as?String A         returndocuments!. stringByAppendingPathComponent (fileName) +     } the  -      $}

iOS development-Data Persistence Swift & model object archiving

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.