1 创建
记得要点上这个
2. 就是模版的建立了。
3. 用于添加的方法。
两种方法。1!!!
<span style="font-size:24px;"> NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
Person *person = [[Person alloc]initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];</span>
<span style="font-size:24px;">
</span>
<span style="font-size:24px;">
</span>
<span style="font-size:24px;">
</span>
2!!
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
我喜欢第二种 就是一句话写成了。
3.就是查找了
NSFetchRequest *fetch = [[NSFetchRequest alloc]initWithEntityName:@"Person"];
//谓词晒选
fetch.predicate = [NSPredicate predicateWithFormat:@"name = '随便'"];
NSArray *array = [self.managedObjectContext executeFetchRequest:fetch error:nil];
比如 我想找person这个里面 条件是名字是随便的中的
查找第二种 就是打上 fetch 就出来语法块了
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
Specify criteria for filtering which objects to fetch
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@ ", @"张三"];
[fetchRequest setPredicate:predicate];
Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
NSError *error = nil;
NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
NSLog(@"error");
};
NSLog(@"%@",fetchedObjects);
Person *suibian = fetchedObjects[0];
[self.managedObjectContext deleteObject:suibian];
每次写完 记得保存
************[self saveContext]*******************
完事了。好用又好玩/
陈佳鸿原创出品