IOS Core Data: stores the Custom object Save Custom NSObject

Source: Internet
Author: User

IOS Core Data: stores the Custom object Save Custom NSObject

Ideas:Convert NSObject to NSData and store 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 = @2000;    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);        }    }
In the Model file, set the person type to Binary Data:

 

 

Both the custom NSObject "Person" and "Birthday" must use NSCoding to implement the initWithCoder and encodeWithCoder methods.

Person

////  Person.h//  CoreDataTest//#import 
 
  #import Birthday.h@interface Person : NSObject
  
   @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 
 
  @interface Birthday : NSObject
  
   @property (strong, nonatomic) NSString *year;@property (strong, nonatomic) NSString *month;@property (strong, nonatomic) NSString *day;- (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

 

 

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.