iOS saves model data (custom model can be stored locally)

Source: Internet
Author: User

1. First create class Category: Inherit from Nsuserdefaults

2 Second premise: Save the model to achieve nscoding

3. Implement both methods in a custom class

-(void) Setcustomobj: (ID) obj forkey: (NSString *) key;

-(ID) Customobjforkey: (NSString *) key;

Implement both methods in a custom class

-(void) Setcustomobj: (ID) obj forkey: (NSString *) key

{

if ([obj respondstoselector: @selector (encodewithcoder:)] = = NO) {

NSLog (@ "Object deposit failed! The object must implement the Encodewithcoder of the Nscoding Protocol: Method ");

Return

}

NSData * Encodeobject = [Nskeyedarchiver archiveddatawithrootobject:obj];

Nsuserdefaults * defaults = [Nsuserdefaults standarduserdefaults];

[Defaults setobject:encodeobject Forkey:key];

[Defaults synchronize];

}

-(ID) Customobjforkey: (NSString *) key

{

Nsuserdefaults * defaults = [Nsuserdefaults standarduserdefaults];

NSData * Encodeobject = [defaults objectforkey:key];

if (Encodeobject = = nil) {

return nil;

}

ID obj = [nskeyedunarchiver unarchiveobjectwithdata:encodeobject];

return obj;

}

4. Come back to our save model to implement the Nscoding protocol

-(ID) Initwithcoder: (Nscoder *) decoder

{

self = [super Initwithcoder:decoder];

if (self) {

Self.cityid = [Decoder decodeobjectforkey:@ "Cityid"];

Self.cityname = [Decoder decodeobjectforkey:@ "CityName"];

Self.citylat = [Decoder decodeobjectforkey:@ "Citylat"];

SELF.CITYLNG = [Decoder decodeobjectforkey:@ "CITYLNG"];

Self.firstcharacter = [Decoder decodeobjectforkey:@ "Firstcharacter"];

Self.subsidiarybankid = [Decoder decodeobjectforkey:@ "Subsidiarybankid"];

}

return self;

}

-(void) Encodewithcoder: (Nscoder *) Acoder

{

[Acoder encodeObject:self.cityId forkey:@ "Cityid"];

[Acoder encodeObject:self.cityName forkey:@ "CityName"];

[Acoder encodeObject:self.cityLat forkey:@ "Citylat"];

[Acoder encodeObject:self.cityLng forkey:@ "CITYLNG"];

[Acoder encodeObject:self.firstCharacter forkey:@ "Firstcharacter"];

[Acoder encodeObject:self.subsidiaryBankId forkey:@ "Subsidiarybankid"];

}

5. Finally came to the controller, the instantiation of the class, you can achieve the preservation model

iOS saves model data (custom model can be stored locally)

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.