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)