about how data is saved (sandbox, archive (Swift method))

Source: Internet
Author: User

Save user information (saved in sandbox)-(void) Saveuserinfo {Nsuserdefaults *defaults = [Nsuserdefaults Standarduserdefaults];//IOS 8.0 will be written directly to the sandbox, no need to use ' [defaults synchronize]; ' [Defaults setobject:

1. archive path, you must use static variable 1. The parameter must be static variable 2 when saving the user account . Static variables need to be loaded only once, and when called, they do not need to be loaded.

static private Let Accountpath = Nssearchpathfordirectoriesindomains (Nssearchpathdirectory.documentdirectory, Nssearchpathdomainmask.userdomainmask, True). last!. Stringbyappendingstring ("Account.plist")

2. Save the user account,

Func Saveaccount () {

Nskeyedarchiver.archiverootobject (self, ToFile:UserAccount.accountPath)

}

Solution-If not saved, the result may still be nil

UserAccount = Nskeyedunarchiver.unarchiveobjectwithfile (Accountpath) as? UserAccount

MARK: Archive (This is the attribute of my own definition)

Func Encodewithcoder (Acoder:nscoder) {

Acoder.encodeobject (Access_token, Forkey: "Access_token")

Acoder.encodedouble (expires_in, Forkey: "expires_in")

Acoder.encodeobject (Expiresdate, Forkey: "Expiresdate")

Acoder.encodeobject (UID, forkey: "UID")

Acoder.encodeobject (name, Forkey: "Name")

Acoder.encodeobject (Avatar_large, Forkey: "Avatar_large")

}

Solution file

Required init? (Coder Adecoder:nscoder) {

Access_token = Adecoder.decodeobjectforkey ("Access_token") as? String

expires_in = Adecoder.decodedoubleforkey ("expires_in")

Expiresdate = Adecoder.decodeobjectforkey ("Expiresdate") as? NSDate

UID = Adecoder.decodeobjectforkey ("UID") as? String

Name = Adecoder.decodeobjectforkey ("name") as? String

Avatar_large = Adecode

}

 
                         

about how data is saved (sandbox, archive (Swift method))

Related Article

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.