zoukankan      html  css  js  c++  java
  • CoreData 添加新字段

    给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;

    }

     

  • 相关阅读:
    Java设计模式之单例模式
    Bootstrap-table使用footerFormatter做统计列
    Bootstrap进度条
    基于Bootstrap的表格插件bootstrap-table
    基于Bootstrap的对话框插件bootstrap-dialog
    基于Bootstrap的下拉框插件bootstrap-select
    JsonConfig处理日期时间
    Jquery表单验证插件validate
    Hibernate使用Criteria去重distinct+分页
    设置iframe高度自适应屏幕高度
  • 原文地址:https://www.cnblogs.com/zhujin/p/5500759.html
Copyright © 2011-2022 走看看