? 相信大家開發的時候都用過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"就行