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)的更多相关文章

  1. ios开发入门篇(一):创建工程

    突然心血来潮,想写点技术方面的东西,做了ios也有好几年了,就简单的写个ios开发的技术博客,希望有人能用得到. 今天就先从创建一个Hellow World工程开始 一:首先打开xcode然后单击Cr ...

  2. CoreData 从入门到精通 (一) 数据模型 + CoreData 栈的创建

    CoreData 是 Cocoa 平台上用来管理模型层数据和数据持久化的一个框架,说简单点,就是一个数据库存储框架.CoreData 里相关的概念比较多,而且初始化也非常繁琐,所以对初学者的学习还是有 ...

  3. iOS 10.0 更新点(开发者视角)

    html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...

  4. [转载]iOS 10 UserNotifications 框架解析

    活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...

  5. iOS 10 :用 UIViewPropertyAnimator 编写动画

    英文:shinobicontrols 译文:戴仓薯 链接:http://www.jianshu.com/p/4244cf130478 [iOS 10 day by day] Day 1:开发 iMes ...

  6. iOS 10 推送的简单使用

    首先介绍一下本文涉及到UserNotifications的几个主要类. 其中 [1]UNNotification主要是作为通知delegate方法的参数使用.包含UNNotificationReque ...

  7. iOS 10 UserNotifications 使用说明

    本教程以贴代码为主.尽可能直观,少量说明. 注意:XCode8的需要手动开启主target Capabilities中的Push Notification. 关于创建多个target后真机测试的证书问 ...

  8. iOS 10 UserNotifications 框架解析

    摘自:https://onevcat.com/2016/08/notification/ iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...

  9. iOS 1 到 iOS 10 ,我都快老了

    iOS 1:iPhone诞生 虽然很难想像,但初代iPhone在问世时在功能方面其实远远落后于那时的竞争对手,比如Windows Mobile.Palm OS.塞班.甚至是黑莓.它不支持3G.多任务. ...

随机推荐

  1. GridView视图

    本文实现如下效果 Test_Grid.java public class Test_Grid extends Activity { private GridView gridview; private ...

  2. wpf纯前台绑定

    <Window x:Class="Example1.MainWindow" ... xmlns:local="clr-namespace:Example1" ...

  3. ASIFormDataRequest 上传图片

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  4. Rotate It !!(思维)

    Rotate It !! Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  5. C#调用WebService实例和开发

    一.基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是 ...

  6. td 单元格 内容自动换行

    <table width="100%" border="1" align="center"> <tr> <td ...

  7. AFNetWorking 关于manager.requestSerializer.timeoutInterval 不起作用的问题

    之前一直遇到关于AFNetWorking请求时间设置了但是不起作用的情况,现用如下方式设置AF的超市时间即可. [manager.requestSerializer willChangeValueFo ...

  8. HttpClient中异步方法的同步调用

    在System.Net.Http中,提供了使用Http与远程服务器通讯的httpClient,但是里面都是异步方法,有时候我们并不需要使用异步操作.这个时候可以使用如下的方式来进行同步调用. clas ...

  9. mysql dos启动出现1067错误的解决方法

    请参看下面的链接:http://www.webjx.com/htmldata/2007-10-16/1192542247.html

  10. android 利用重力感应监听 来电时翻转手机后静音。

       在CallNotifier.java中 加入如下代码: public void GetSensorManager(Context context) { sm = (SensorManager) ...