使用coreData

1、设计数据模型

2、创建持久化视图和控制器

 1 #import "BIDViewController.h"
 2 #import "BIDAppDelegate.h"
 3 
 4 static NSString * const kLineEntityName = @"Line";
 5 static NSString * const kLineNumberKey = @"lineNumber";
 6 static NSString * const kLineTextKey = @"lineText";
 7 
 8 @interface BIDViewController ()
 9 
10 @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *lineFields;
11 
12 @end
13 
14 @implementation BIDViewController
15 
16 - (void)viewDidLoad
17 {
18     [super viewDidLoad];
    //获取应用委托的引用,使用引用获得创建托管对象上下文。
20 BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate; 21 NSManagedObjectContext * context = [appDelegate managedObjectContext];

    //创建一个获取请求并将实体描述传递给它,以便请求知道要检索的对象类型。
22 NSFetchRequest * request = [[NSFetchRequest alloc] 23 initWithEntityName:kLineEntityName];
24 //通过执行没有谓语的请求,上下文将返回库中的每一个Line对象。 25 NSError * error; 26 NSArray * objects = [context executeFetchRequest:request error:&error]; 27 if (objects == nil) { 28 NSLog(@"There was an error!"); 29 // Do whatever error handling is appropriate 30 } 31
    //遍历以获取托管对象数组,从中提取每个托管对象的lineNumber和lineText值,并使用信息更新界面的文本框。 32 for (NSManagedObject * oneObject in objects) { 33 int lineNum = [[oneObject valueForKey:kLineNumberKey] intValue]; 34 NSString *lineText = [oneObject valueForKey:kLineTextKey]; 35 36 UITextField *theField = self.lineFields[lineNum]; 37 theField.text = lineText; 38 } 39 40 UIApplication *app = [UIApplication sharedApplication]; 41 [[NSNotificationCenter defaultCenter] 42 addObserver:self 43 selector:@selector(applicationWillResignActive:) 44 name:UIApplicationWillResignActiveNotification 45 object:app]; 46 } 47 48 - (void)applicationWillResignActive:(NSNotification *)notification 49 {
    //先获取对应的委托引用,然后使用此引用获取指向应用的默认上下文指针。
50 BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate; 51 NSManagedObjectContext * context = [appDelegate managedObjectContext]; 52 NSError *error; 53 for (int i = 0; i < 4; i++) { 54 UITextField *theField = self.lineFields[i];
       //为line实体创建获取请求,创建一个谓语,确认持久存储中是否已经有一个与这个字段对应的托管对象。
56 NSFetchRequest *request = [[NSFetchRequest alloc] 57 initWithEntityName:kLineEntityName]; 58 NSPredicate *pred = [NSPredicate 59 predicateWithFormat:@"(%K = %d)", kLineNumberKey, i]; 60 [request setPredicate:pred]; 61 //在上下文中执行获取请求 62 NSArray * objects = [context executeFetchRequest:request error:&error]; 63 if (objects == nil) { 64 NSLog(@"There was an error!"); 65 // Do whatever error handling is appropriate 66 }
67 //申明一个指向NSManagedObject的指针并将它设置为nil。检查返回值对象objects,如果存在有效对象就加载,否则创建一个新的托管对象来保存这个字段的文本。 68 NSManagedObject * theLine = nil; 69 if ([objects count] > 0) { 70 theLine = [objects objectAtIndex:0]; 71 } else { 72 theLine = [NSEntityDescription 73 insertNewObjectForEntityForName:kLineEntityName 74 inManagedObjectContext:context]; 75 } 76 //使用键——值编码来设置行号以及此托管对象的文本。 77 [theLine setValue:[NSNumber numberWithInt:i] forKey:kLineNumberKey]; 78 [theLine setValue:theField.text forKey:kLineTextKey]; 80 }
    //通知上下文保存修改。
81 [appDelegate saveContext]; 82 }
原文地址:https://www.cnblogs.com/fengmin/p/5382587.html