iOS Development note-swift archiving for iOS data persistence nskeyedarchiver

Source: Internet
Author: User

There are three ways to persist iOS data:

    1. Attribute list (plist, nsuserdefaults)
    2. Archive (Nskeyedarchiver)
    3. Databases (SQLite, Core Data, third-party class libraries, etc.)

Archive (aka serialization), convert objects to bytecode, save them to disk as files, and restore them by unpacking (deserializing) the program as it runs or when it is rewritten again. This article mainly introduces the swift implementation of iOS data archiving.

Archiving Foundation Framework Objects

func Archivedata () {var path:anyobject=nssearchpathfordirectoriesindomains (Nssearchpathdirectory.documentdirectory, Nssearchpathdomainmask.userdomainmask,true)[0] Var filePath=path.stringbyappendingpathcomponent ("data.archive")        //Archivevar array=["Bill Gates","Steve Jobs"]        if(Nskeyedarchiver.archiverootobject (array, Tofile:filepath)) {NSLog ("Archive Success")}}} func Unarchivedata () {var path:anyobject=nssearchpathfordirectoriesindomains (Nssearchpathdirectory.documentdirectory, Nssearchpathdomainmask.userdomainmask,true)[0] Var filePath=path.stringbyappendingpathcomponent ("data.archive")        //Anti-archivingvar data=nskeyedunarchiver.unarchiveobjectwithfile (FilePath) asNsarray NSLog ("%@", data)}

Summary:

  1. Easy archiving and archiving procedures
  2. Only one object can be archived at a time, if multiple object archives need to be separated
  3. Archived objects are objects in the foundation framework
  4. Archive and archive any of these objects need to be archived and archived throughout the file.
  5. Archived files are encrypted, so the file extension is free to take
Archive Custom Data
var path=nssearchpathfordirectoriesindomains (Nssearchpathdirectory.documentdirectory, Nssearchpathdomainmask.userdomainmask,true)[0] asnsstring var filePath=path.stringbyappendingpathcomponent ("data.archive")        //Archivevar data=nsmutabledata () var archiver=nskeyedarchiver (Forwritingwithmutabledata:data) archiver.encodeobject (["Bill Gates","Steve Jobs"], Forkey:"Data"); Archiver.encodeint ( +, Forkey:" Age"); Archiver.encodeobject ("Test Message", Forkey:"Tip"); Archiver.finishencoding () data.writetofile (FilePath, atomically:true)         //Anti-archivingvar unarchivedata=NSData (Contentsoffile:filepath) var unarchiver=nskeyedunarchiver (forreadingwithdata:unarchivedata!) var decodedata=unarchiver.decodeobjectforkey ("Data") asnsarray var decodeage=unarchiver.decodeintforkey (" Age") var decodetip=unarchiver.decodeobjectforkey ("Tip") asnsstring NSLog ("data=%@,age=%i,tip=%@", Decodedata,decodeage,decodetip)

Summary:

  1. In a keyed archive, each archive field has a key value that matches the key value when it is archived
  2. With key archive You can store multiple objects at once
  3. Archived objects are objects in the foundation framework
  4. Archive and archive any of these objects need to be archived and archived throughout the file.
  5. Archived files are encrypted, so the file extension is free to take

iOS Development note-swift archiving for iOS data persistence nskeyedarchiver

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.