首页 > 代码库 > CoreData简单教学(二)

CoreData简单教学(二)

1.CoreData 编写方便, 管理方便, 出错不易寻找, 整体性强, 关联性强

2.简单介绍查询的办法

 

 1 #pragma mark -  2 - (NSMutableArray *) findAll { 3     NSManagedObjectContext * cxt = [self managedObjectContext]; 4      5     NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:cxt]; 6     NSFetchRequest * request = [[NSFetchRequest alloc] init]; 7     [request setEntity:entityDescription]; 8      9     NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];10     [request setSortDescriptors:@[sortDescriptor]];11     12     NSError * error = nil;13     NSArray * listData = http://www.mamicode.com/[cxt executeFetchRequest:request error:&error];14     NSMutableArray * resListArray = [[NSMutableArray alloc] init];15     16     for (Person * handle in listData) {17         Person * person = [[Person alloc] init];18         person.name = handle.name;19         person.age = handle.age;20         21         [resListArray addObject:person];22     }23     24     return resListArray;25 }

 

CoreData简单教学(二)