给CoreData添加新属性,就是给数据库加新字段,那么必须要进行数据库版本升级及CoreData数据迁移;

具体操作是

1.选择DemoCoreData.xcdatamodeld 文件,Editor ->Add Model Version ,输入新的版本名字;

2.在右侧的文件查看器窗口 的Model Version   current 设置当前最新的版本名字;

3.在新数据模型的文件上添加字段,记得在类文件里也要添加上你添加的新字段,可以删除原来的类文件,重新生成manageobject类。

4.代码中加入 数据迁移的配置选项:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.

//设置CoreData迁移配置

NSDictionary *options = [NSDictionarydictionaryWithObjectsAndKeys:@YES,NSInferMappingModelAutomaticallyOption, @YES,NSMigratePersistentStoresAutomaticallyOption, nil];

if (_persistentStoreCoordinator != nil) {

return_persistentStoreCoordinator;

}

// Create the coordinator and store

_persistentStoreCoordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:[selfmanagedObjectModel]];

NSURL *storeURL = [[selfapplicationDocumentsDirectory] URLByAppendingPathComponent:@"DemoCoreData.sqlite"];

NSError *error = nil;

NSString *failureReason = @"There was an error creating or loading the application's saved data.";

if (![_persistentStoreCoordinatoraddPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:storeURL options:options error:&error]) {

// Report any error we got.

NSMutableDictionary *dict = [NSMutableDictionarydictionary];

dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";

dict[NSLocalizedFailureReasonErrorKey] = failureReason;

dict[NSUnderlyingErrorKey] = error;

error = [NSErrorerrorWithDomain:@"YOUR_ERROR_DOMAIN"code:9999userInfo:dict];

// Replace this 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();

}

return_persistentStoreCoordinator;

}

CoreData 添加新字段的更多相关文章

  1. ASP.NET MVC 5 - 给电影表和模型添加新字段

    在本节中,您将使用Entity Framework Code First来实现模型类上的操作.从而使得这些操作和变更,可以应用到数据库中. 默认情况下,就像您在之前的教程中所作的那样,使用 Entit ...

  2. 【译】ASP.NET MVC 5 教程 - 9:添加新字段

    原文:[译]ASP.NET MVC 5 教程 - 9:添加新字段 在本节中,我们将使用Entity Framework Code First 数据迁移功能将模型类的改变应用到数据库中. 默认情况下,当 ...

  3. 学习ASP.NET Core Razor 编程系列十——添加新字段

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  4. Django数据库,在原有表中添加新字段

    1.在你要添加新字段的app的 models.py 文件中添加需要新增的字段(这里新增的是dress字段): from django.db import models # Create your mo ...

  5. SqlServer表中添加新字段

    表中添加新字段ALTER TABLE 表名 ADD 字段名 VARCHAR(20) NULL 表中添加自增idalter table lianxi add id int primary key IDE ...

  6. [转]ASP.NET MVC 5 - 给电影表和模型添加新字段

    在本节中,您将使用Entity Framework Code First来实现模型类上的操作.从而使得这些操作和变更,可以应用到数据库中. 默认情况下,就像您在之前的教程中所作的那样,使用 Entit ...

  7. EF之DataBase添加新字段

    数据库中表添加新字段后,在EF的xml格式的中找到与表名相同的节点添加新字段 (SSDL.CSDL和C-S都要添加相关信息)

  8. SQL SERVER 表添加新字段

    SQL SERVER 表添加新字段 ALTER TABLE doc_exa ADD column_b VARCHAR(20) NULL; -- doc_exa 是表名 -- column_b 是新加的 ...

  9. mysql在某一字段后面添加新字段

    mysql在某一字段后面添加新字段 ALTER TABLE `test` ADD `anyField` TEXT NOT NULL AFTER `id`

随机推荐

  1. Java:多线程<二> 同步

    由于多线程的访问出现延迟和线程的随机性,在使用多线程时往往会伴随安全性的问题,这些问题一旦出现将会是非常严重的.为了解决这种安全性问题,synchronized出现了. synchronized用法一 ...

  2. javascript 继承实现

    JavaScript高级程序设计读书笔记 这本书继承的实现列举了好几种,里面有两种是比较靠谱的. 1 组合继承 //父构造函数 function Super(name) { this.name = n ...

  3. STL模板之_map,stack(计算矩阵相乘的次数)

    #include <map>#include <stack>#include <iostream>using namespace std; struct Node ...

  4. Oracle Metadata Management (OMM)元数据管理 12.2.1发布

    元数据管理元数据管理是解决大量关键业务和技术挑战的基础,这些挑战包括元数据实体有多少,上游数据变化的影响,在浏览器中提供友好的分析展现界面,或提供企业范围内的元数据现状分析和改进视图.OMM是一款基于 ...

  5. Modbus工业协议在Android中的应用

    现在工业信息画发展,很多工厂都需要做信息化展示,通常都是利用Android一体机来进行展示和交互. Modbus协议是全球第一个用于工业现场的总线协议,与外设交互可以采用串口通信,tcp等方式:通常在 ...

  6. 第十三周学习笔记(编辑器选错了重发了这一个 原博客的确周天晚上提交了orz)

    并发:逻辑控制流在时间上的重叠. 构造并发程序的方法: 进程 I/O多路复用 线程 基于进程的并发编程 假设我们有两个客户端和一个服务器,服务器正在监听一个监听表述符上的请求.现在假设服务器接受了客户 ...

  7. 简单的JS多物体的运动---运动和透明度的变化

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. C++中 destory() 和deallocate()以及delete函数的相关性和区别性

    这里非常的绕口  需要仔细的来看看: destory(): 显示调用一个对象的析构函数 相当于释放一个对象需要释放的一些动态内存 为下次真正释放对象做准备 deallocate():真正的释放一个内存 ...

  9. Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  10. Welcome to LED Control Wiki

    About this project This project was developed after I had to find out that controlling my RGB ambien ...