给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. Oracle中建立表

    -- Create table create table STUDENT( sno VARCHAR2(3) not null, sname VARCHAR2(8) not null, ssex VAR ...

  2. 大数运算Swift

    前几天开始,打算用Swift写大数的运算,加法跟乘法都已经写好了,写减法发现,真是难,感觉有可能是我的想法不对?不不不我相信我的逻辑. 首先把数字分成小数部分跟整数部分,再遍历一下,识别当前的结果,是 ...

  3. Flex文件结构

    一.文件.目录及其作用.project:描述工程信息,如 本工程名称.工程注释.相关工程信息.编译参数等 .flexProperties:记录与Flex本身相关的信息 .actionScriptPro ...

  4. hibernate 的 CascadeType 属性

    hibernateintegerstringfloatclassmerge @Entity @Table(name="orders")public class Order { // ...

  5. Ambari是什么?

    Ambari目标 解决Hadoop生态系统部署 部署:hadoop组件间有依赖,包括配置.版本.启动顺序.权限配置等. 部署过程跟踪.能够展示出部署过程中每个步骤的状态及相关信息. 多机部署问题,当集 ...

  6. 标准IO的简单应用,动静态库,读取系统时间并打印,模拟ls -l功能

    2015.2.27星期五,小雨 标准IO实现的复制功能: #include <stdio.h>#include <errno.h> #define N 64 int main( ...

  7. QT_BEGIN_NAMESPACE QT_END_NAMESPACE

    QT_BEGIN_NAMESPACEQT_END_NAMESPACE 在源代码中是这样定义的: 1 2 # define QT_BEGIN_NAMESPACE namespace QT_NAMESPA ...

  8. iOS 利用 Framework 进行动态更新

    http://nixwang.com/2015/11/09/ios-dynamic-update/ 前言 目前 iOS 上的动态更新方案主要有以下 4 种: HTML 5 lua(wax)hotpat ...

  9. 在线工具、setHtmlRem、px2rem

    http://tool.lu/c/developer  开发类在线工具 https://github.com/leon776/setHtmlRem   setHtmlRem https://githu ...

  10. IOS网络开发(三)

    1 飞机航班查询软件 1.1 问题 NSURLConnection是IOS提供的用于处理Http协议的网络请求的类,可以实现同步请求也可以实现异步请求,本案例使用NSURLConnection类实现一 ...