iOS 10 (X8)上CoreData的使用(包含创建工程时未添加CoreData)
1.在创建工程时未添加CoreData,后期想要使用CoreData则要在工程Appdelegate.h文件中添加CoreData库和CoreData中的通道类(用来管理类实例和CoreData之间的所有操作)和保存到CoreData文件的方法.

2.添加完这些后去创建.xcdatamodeld文件

3.填写创建文件的名称(建议与工程名字一致后面添加CoreData)

4.文件创建完成后就可以像以前一样去创建对应实体文件和添加实体的属性了.

5.添加完实体后生成对应的实体类文件

6.创建完对应的实体类文件后回到Appdelegate.m中去实现添加的方法和实例
.首先在Appdelegate.m的- (void)applicationWillTerminate:(UIApplication *)application;中调用下保存的方法.
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[self saveContext];
}
.然后实现通道类和保存的方法.
#pragma mark - Core Data stack
@synthesize persistentContainer = _persistentContainer;
- (NSPersistentContainer *)persistentContainer {
// The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
@synchronized (self) {
if (_persistentContainer == nil) {
_persistentContainer = [[NSPersistentContainer alloc] initWithName:@"LotterSelectCoreData"];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}];
}
}
return _persistentContainer;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *context = self.persistentContainer.viewContext;
NSError *error = nil;
if ([context hasChanges] && ![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}
7.至于新版Xcode 中coredata的使用在下篇博文中会有介绍.
iOS 10 (X8)上CoreData的使用(包含创建工程时未添加CoreData)的更多相关文章
- ios开发入门篇(一):创建工程
突然心血来潮,想写点技术方面的东西,做了ios也有好几年了,就简单的写个ios开发的技术博客,希望有人能用得到. 今天就先从创建一个Hellow World工程开始 一:首先打开xcode然后单击Cr ...
- CoreData 从入门到精通 (一) 数据模型 + CoreData 栈的创建
CoreData 是 Cocoa 平台上用来管理模型层数据和数据持久化的一个框架,说简单点,就是一个数据库存储框架.CoreData 里相关的概念比较多,而且初始化也非常繁琐,所以对初学者的学习还是有 ...
- iOS 10.0 更新点(开发者视角)
html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...
- [转载]iOS 10 UserNotifications 框架解析
活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- iOS 10 :用 UIViewPropertyAnimator 编写动画
英文:shinobicontrols 译文:戴仓薯 链接:http://www.jianshu.com/p/4244cf130478 [iOS 10 day by day] Day 1:开发 iMes ...
- iOS 10 推送的简单使用
首先介绍一下本文涉及到UserNotifications的几个主要类. 其中 [1]UNNotification主要是作为通知delegate方法的参数使用.包含UNNotificationReque ...
- iOS 10 UserNotifications 使用说明
本教程以贴代码为主.尽可能直观,少量说明. 注意:XCode8的需要手动开启主target Capabilities中的Push Notification. 关于创建多个target后真机测试的证书问 ...
- iOS 10 UserNotifications 框架解析
摘自:https://onevcat.com/2016/08/notification/ iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- iOS 1 到 iOS 10 ,我都快老了
iOS 1:iPhone诞生 虽然很难想像,但初代iPhone在问世时在功能方面其实远远落后于那时的竞争对手,比如Windows Mobile.Palm OS.塞班.甚至是黑莓.它不支持3G.多任务. ...
随机推荐
- SharePoint excel service web part 连接到 filter web part
本文讲述SharePoint excel service web part 连接到 filter web part的一个简单应用场景. SharePoint excel service web par ...
- ASIFormDataRequest 上传图片
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- hadoop之mapreduse 在Eclipse下的调试环境篇
搭建完毕环境后,開始调试mapreduse程序. 可是遇到不停的报错.本人非常讨厌在自己的操作系统环境变量里设置来设置去,包含linux也是. 通常喜欢把设置环境变量在启动程序的脚本中.让脚本自己执行 ...
- WEB服务器2--IIS架构(转)
开始之前可以先读:http://www.cnblogs.com/tiantianle/p/5419445.html 原文:http://www.cnblogs.com/arbin98/archive/ ...
- NET中级课--文件,流,序列化3
1.序列化:将对象及状态保存起来. 反序列化就是逆操作. 2.NET提供了一个接口:System.runtime.serialization.IFormatter接口, 还有实现了这个接口的类Bina ...
- CLR via C# - GC
//像背书一样,记录下吧 1.CLR分配资源 托管堆上维护着一个指针NextObjPtr.该指针表示下一个新建对象在托管堆上的位置.C#的new Object会产生IL newobj指令,NextOb ...
- python os模块文件相关
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
- 学习AJAX(二)
- 在IIS Express中调试时无法读取配置文件
在IIS Express中调试代码时,如果出现“无法读取配置文件”的问题(如图),这种情况是IIS Express的“applicationhost.config”配置文件中的映射关系出了问题[ps: ...
- asp.net 内部重定向
1. /* * 2. * Context.RewritePath() * 使用给定路径重写 URL.(内部重写) * 内部请求重写 */ public static void TestTwo() { ...