[Xcode] NSUserDefaults standardUserDefaults save an NSobject in NSUserdefault.
lundi 18 janvier 2016Cet article vous permettra de sauver un objet dans NSUserDefaults:
Fonctions à appeler:
- (void) save_cforder:(cfordersResponse *)result{ NSData *personEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:result]; [[NSUserDefaults standardUserDefaults] setObject:personEncodedObject forKey:@"cforder"]; } - (cfordersResponse *) getcforder{ NSData * data = [[NSUserDefaults standardUserDefaults] objectForKey:@"cforder"]; cfordersResponse * cforder = [NSKeyedUnarchiver unarchiveObjectWithData:data]; return cforder; }
.h @interface cfordersResponse : NSObject <NSCoding> .m - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.num_cforder = [aDecoder decodeObjectForKey:@"num_cforder"]; self.num_cforder_min = [aDecoder decodeObjectForKey:@"num_cforder_min"]; self.date_cforder = [aDecoder decodeObjectForKey:@"date_cforder"]; self.lastname_customer = [aDecoder decodeObjectForKey:@"lastname_customer"]; self.firstname_customer = [aDecoder decodeObjectForKey:@"firstname_customer"]; self.shopname = [aDecoder decodeObjectForKey:@"shopname"]; self.order_id = (int)[aDecoder decodeIntegerForKey:@"order_id"]; self.in_order = [aDecoder decodeObjectForKey:@"in_order"]; self.order_ended = [aDecoder decodeObjectForKey:@"order_ended"]; self.shopid = [aDecoder decodeObjectForKey:@"shopid"]; self.major = (int)[aDecoder decodeObjectForKey:@"major"]; self.minor = (int)[aDecoder decodeObjectForKey:@"minor"]; self.phone = [aDecoder decodeObjectForKey:@"phone"]; self.latitude = [aDecoder decodeObjectForKey:@"latitude"]; self.longitude = [aDecoder decodeObjectForKey:@"longitude"]; self.message = [aDecoder decodeObjectForKey:@"message"]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_num_cforder forKey:@"num_cforder"]; [aCoder encodeObject:_num_cforder_min forKey:@"num_cforder_min"]; [aCoder encodeObject:_date_cforder forKey:@"date_cforder"]; [aCoder encodeObject:_lastname_customer forKey:@"lastname_customer"]; [aCoder encodeObject:_firstname_customer forKey:@"firstname_customer"]; [aCoder encodeObject:_shopname forKey:@"shopname"]; [aCoder encodeInteger:_order_id forKey:@"order_id"]; [aCoder encodeBool:_in_order forKey:@"in_order"]; [aCoder encodeBool:_order_ended forKey:@"order_ended"]; [aCoder encodeObject:_shopid forKey:@"shopid"]; [aCoder encodeInteger:_major forKey:@"major"]; [aCoder encodeInteger:_minor forKey:@"minor"]; [aCoder encodeObject:_phone forKey:@"phone"]; [aCoder encodeObject:_latitude forKey:@"latitude"]; [aCoder encodeObject:_longitude forKey:@"longitude"]; [aCoder encodeObject:_message forKey:@"message"]; }