//加载托管对象模型(coreData数据模型文件。)
NSString *momdPath = [[NSBundle mainBundle]pathForResource:@"Person" ofType:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:momdPath]];
//创建持久化存储协调器,处理数据的读写
NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:mom];
//指定sqlite数据库文件的存储路径(coreData使用的数据库文件后缀一般写sqlite)
NSString *dbPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/test.sqlite"];
//将coreData文件映射到数据库,并判断操作状态
NSError *createError = nil;
NSPersistentStore *store = [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dbPath] options:nil error:&createError];
if (!store) {//出错,打印错误信息localizedDescription
NSLog(@"add error: %@", createError.description);
}
//创建操作数据的对象()
_managedObjectContext = [[NSManagedObjectContext alloc]init];
//关联持久化存储协调器
_managedObjectContext.persistentStoreCoordinator = storeCoordinator;
/*增*/
//在数据库中插入一个实体(模型)
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_managedObjectContext];
//对实体的属性赋值
person.name = _textName.text;
person.age = [NSNumber numberWithInteger:_textAge.text.integerValue];
//把插入的实体保存到数据库中
NSError *insertError = nil;
if (![_managedObjectContext save:&insertError]) {
NSLog(@"insert error: %@", insertError.description);
} else {//插入成功
[_persons addObject:person];
[_tableV reloadData];
}
//删除
if (_persons.count > 0) {//确保有数据才删除
Person *person = _persons[_currentRow];
//从数据库中删除并保存
[_managedObjectContext deleteObject:person];
NSError *saveError = nil;
if (![_managedObjectContext save:&saveError]) {
NSLog(@"delete save error: %@", saveError.description);
} else {
//从本地数组中删除
[_persons removeObject:person];
[_tableV reloadData];
}
}
//修改
Person *person = _persons[_currentRow];
//先修改对象的属性
person.name = _textName.text;
person.age = [NSNumber numberWithInteger: _textAge.text.integerValue];
//再存入数据库
NSError *updateError = nil;
if (![_managedObjectContext save:&updateError]) {//保存失败
NSLog(@"update save error: %@", updateError.description);
} else {//保存成功
[_tableV reloadData];
}
//查
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
//定义查询条件的谓词(加*号的意思是以zh开头)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@", @"zh*"];
//设置fetchRequest的谓词//如果不设查询条件会查询所有
[fetchRequest setPredicate:predicate];
//执行查询
NSArray *fetchResult = [_managedObjectContext executeFetchRequest:fetchRequest error:nil];
[_persons setArray:fetchResult];
[_tableV reloadData];
苹果图标规范
30分钟精通正则表达式
http://deerchao.net/tutorials/regex/regex.htm
UI特效