相信大家开发的时候都用过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"就行