coredata的各种报错

  相信大家开发的时候都用过coredata,用来保存数据挺方便的。只是问题是,突然发现,如果最开始创建项目的时候没有选取coredata,那么之后要用的话就需要自己手动添加代码到appdelegate里面了,同时也带来了一系列的报错。

- (NSManagedObjectModel *)managedObjectModel {

// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.

if (_managedObjectModel != nil) {

return _managedObjectModel;

}

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"对应的名字" withExtension:@"momd"];

_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

return _managedObjectModel;

}

  第一个问题就是managedobjectmodel,如果是复制粘贴的话,这里是必须要改的,url要改为和自己的momd对应的名字。

- (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.

if (_persistentStoreCoordinator != nil) {

return _persistentStoreCoordinator;

}

// Create the coordinator and store

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"对应的名字.sqlite"];

NSError *error = nil;

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

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

// Report any error we got.

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

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

dict[NSLocalizedFailureReasonErrorKey] = failureReason;

dict[NSUnderlyingErrorKey] = error;

error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo: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;

}

  第二个问题,储存器的路径也记得要改,因为复制过来肯定不是现项目的路径,所以最好改为自己项目的名称。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

self.managedObjectContext = [appDelegate managedObjectContext];

self.managedObjectModel = [appDelegate managedObjectModel];

第三个问题,要记得传递managedobjectcontext,要不储存的时候必定会报错。

#import "RUN.h"    NS_ASSUME_NONNULL_BEGIN    

@interface RUN (CoreDataProperties)    

@property (nullable, nonatomic, retain) NSDate *date; 

 @property (nullable, nonatomic, retain) NSNumber *distance;  

@property (nullable, nonatomic, retain) NSNumber *duration;  

@property (nullable, nonatomic, retain) NSOrderedSet*locations;    

@end    @interface RUN (CoreDataGeneratedAccessors)    

- (void)insertObject:(Location *)value inLocationsAtIndex:(NSUInteger)idx;  

- (void)removeObjectFromLocationsAtIndex:(NSUInteger)idx; 

 - (void)insertLocations:(NSArray*)value atIndexes:(NSIndexSet *)indexes; 

 - (void)removeLocationsAtIndexes:(NSIndexSet *)indexes;  

- (void)replaceObjectInLocationsAtIndex:(NSUInteger)idx withObject:(Location *)value;  

- (void)replaceLocationsAtIndexes:(NSIndexSet *)indexes withLocations:(NSArray*)values;  

- (void)addLocationsObject:(Location *)value;  

- (void)removeLocationsObject:(Location *)value;  

- (void)addLocations:(NSOrderedSet*)values;  

- (void)removeLocations:(NSOrderedSet*)values;

@end

NS_ASSUME_NONNULL_END

  iOS9生成managed object的话,会生成4个文件,以“RUN”举例子的话,就是RUN.h、RUN.m、RUN+CoreDataProperties.h、RUN+CoreDataProperties.m。其实区别不大,RUN+CoreDataProperties里主要代码如上图所示,大家注意一下第二句和最后一句,这是OC为了配合swift的一些特性苹果在iOS9添加的,表明这些属性可否为nil。使用的时候和以前没区别,#import "RUN.h"就行

推荐阅读更多精彩内容

  • 本人ios初学者,为自己学习方便,复制各位大神的学习性文章放在自己简书里,仅作为自己学习方便使用,如果作者疑此行为...
    bu再等阅读 210评论 0 0
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 152评论 0 0
  • ###修改 UITableview 滚动条颜色的方法 UITableview的滚动条默认颜色是黑色的,如果UIta...
    uncleRX阅读 538评论 0 2
  • 1.简介 1)coreData提供了对象持久化管理,不需要关心数据的内部存储,只需要关心对象的增删查改. FM...
    _skye阅读 4,094评论 3 26
  • 学习,就像上台阶,需要一步一步上,如果台阶太高,我们要给孩子中间铺垫很多小台阶。对于有些知识,尤其是特别难的知识,...
    彭晓芬阅读 138评论 0 2