idea: convert NSObject to NSData and then deposit NSData into core data
Core Data Implementation
Add Data:
Appdelegate *appdelegate = [[UIApplication sharedapplication] delegate]; Nsmanagedobjectcontext *context = [Appdelegate managedobjectcontext]; Nsmanagedobject *newcontact; NewContact = [nsentitydescription insertnewobjectforentityforname:@ "Contact" inmanagedobjectcontext:context]; Person *person = [[Person alloc] init]; Person.name = @ "Xiaoming"; Person.number = @ "1008610086"; Birthday *birthday = [[Birthday alloc] init]; Birthday.year = @ "+"; Birthday.month = @ "January"; Birthday.day = @ "1st"; Person.birthday = Birthday; NSData *data = [Nskeyedarchiver Archiveddatawithrootobject:person]; [NewContact setvalue:data forkey:@ "person"]; Nserror *error; [Context save:&error];
Get Data:
Appdelegate *appdelegate = [[UIApplication sharedapplication] delegate]; Nsfetchrequest *request = [[Nsfetchrequest alloc] init]; Nsmanagedobjectcontext *context = [Appdelegate managedobjectcontext]; Nsentitydescription *entitydesc = [nsentitydescription entityforname:@ "Contact" inmanagedobjectcontext:context]; [Request Setentity:entitydesc]; Nsmanagedobject *matches = nil; Nserror *error; Nsarray *objects = [Context executefetchrequest:request error:&error]; if ([objects count] = = 0) {NSLog (@ "No matches"); } else {for (int i = 0; i < [objects count]; i++) {matches = objects[i]; NSData *data = [matches valueforkey:@ "person"]; Person *person = [Nskeyedunarchiver unarchiveobjectwithdata:data]; NSLog (@ "My name is%@. I was born on%@%@,%@. My phone number is%@ ", Person.name, Person.birthday.month, Person.birthday.day, Person.birthday.year, Person.number); } }
the type of person in the model file is set to binary Data:
Both the person and birthday, both custom nsobject, need to use nscoding to implement Initwithcoder and Encodewithcoder two methods
Person
person.h// coredatatest//#import <Foundation/Foundation.h> #import "Birthday.h" @interface Person:nsobject<nscoding> @property (Strong, nonatomic) NSString *name; @property (Strong, nonatomic) NSString * number; @property (strong, Nonatomic) Birthday *birthday;-(ID) init; @end
person.m// coredatatest//#import "Person.h" @implementation person-(ID) init{ self.name = nil; Self.number = nil; return self;} -(ID) Initwithcoder: (Nscoder *) decoder{ if (self = [super init]) { Self.name = [Decoder decodeobjectforkey: @ "name"]; Self.number = [decoder Decodeobjectforkey: @ "number"]; Self.birthday = [Decoder Decodeobjectforkey: @ "Birthday"]; } return self;} -(void) Encodewithcoder: (Nscoder *) encoder{ [encoder encodeObject:self.name forkey:@ "name"]; [Encoder encodeObject:self.number forkey:@ "number"]; [Encoder encodeObject:self.birthday forkey:@ "Birthday"];} @end
Birthday
////Birthday.h coredatatest//#import <Foundation/Foundation.h> @interface birthday:nsobject<nscoding> @property ( Strong, Nonatomic) NSString *year, @property (Strong, nonatomic) NSString *month; @property (Strong, nonatomic) NSString *d ay;-(ID) init; @end
birthday.m// coredatatest//#import "Birthday.h" @implementation birthday-(ID) init{ self.year = nil; Self.month = nil; Self.day = nil; return self;} -(ID) Initwithcoder: (Nscoder *) decoder{ if (self = [super init]) { self.year = [decoder Decodeobjectforkey: @ "Year"]; Self.month = [decoder decodeobjectforkey: @ "month"]; Self.day = [Decoder Decodeobjectforkey: @ "Day"]; } return self;} -(void) Encodewithcoder: (Nscoder *) encoder{ [encoder encodeObject:self.year forkey:@ "year"]; [Encoder encodeObject:self.month forkey:@ "Month"]; [Encoder encodeObject:self.day forkey:@ "Day"];} @end
Reference Links:
Http://sam.roon.io/archiving-objective-c-objects-with-nscoding
Https://coderwall.com/p/mx_wmq/how-to-save-a-nsarray-nsmutablearray-in-core-data
http://pinkstone.co.uk/how-to-save-a-uiimage-in-core-data-and-retrieve-it/
IOS Core Data: Store custom objects Save Customize NSObject