码迷,mamicode.com
首页 > 移动开发 > 详细

iOS Core Data: 存储自定义对象 Save Custom NSObject

时间:2014-11-04 19:39:23      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   os   使用   for   

思路:将NSObject转化为NSData,然后将NSData存入到Core Data中

Core 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];
获取数据:
    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);
        }
    }
Model文件中设置person的类型设为Binary Data:

bubuko.com,布布扣

Person和Birthday这两个自定的NSObject,都需要使用NSCoding,实现initWithCoder和encodeWithCoder两个方法

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 *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


参考链接:

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: 存储自定义对象 Save Custom NSObject

标签:des   style   blog   http   io   ar   os   使用   for   

原文地址:http://blog.csdn.net/willyang519/article/details/40790075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!