coredata的各種報錯

? 相信大家開發(fā)的時候都用過coredata,用來保存數(shù)據(jù)挺方便的。只是問題是,突然發(fā)現(xiàn),如果最開始創(chuàng)建項目的時候沒有選取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:@"對應(yīng)的名字" withExtension:@"momd"];

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

return _managedObjectModel;

}

? 第一個問題就是managedobjectmodel,如果是復(fù)制粘貼的話,這里是必須要改的,url要改為和自己的momd對應(yīng)的名字。

- (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:@"對應(yīng)的名字.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;

}

? 第二個問題,儲存器的路徑也記得要改,因為復(fù)制過來肯定不是現(xiàn)項目的路徑,所以最好改為自己項目的名稱。

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。其實區(qū)別不大,RUN+CoreDataProperties里主要代碼如上圖所示,大家注意一下第二句和最后一句,這是OC為了配合swift的一些特性蘋果在iOS9添加的,表明這些屬性可否為nil。使用的時候和以前沒區(qū)別,#import "RUN.h"就行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 本人ios初學(xué)者,為自己學(xué)習(xí)方便,復(fù)制各位大神的學(xué)習(xí)性文章放在自己簡書里,僅作為自己學(xué)習(xí)方便使用,如果作者疑此行為...
    bu再等閱讀 686評論 0 0
  • 哦吼吼,又研究了幾天,把FMDB這個封裝好的數(shù)據(jù)庫搞定了,寫了個簡單的例子,基于FMDB的添刪改查操作,界面很一般...
    lichengjin閱讀 571評論 0 0
  • 1.簡介 1)coreData提供了對象持久化管理,不需要關(guān)心數(shù)據(jù)的內(nèi)部存儲,只需要關(guān)心對象的增刪查改. FM...
    _skye閱讀 4,602評論 3 26
  • 學(xué)習(xí),就像上臺階,需要一步一步上,如果臺階太高,我們要給孩子中間鋪墊很多小臺階。對于有些知識,尤其是特別難的知識,...
    彭曉芬閱讀 459評論 0 2
  • 每天5個小時,如果你是用來看韓劇、翻手機、玩游戲,那么7年后,你會變成一個生活的旁觀者,你最擅長的就是如數(shù)家...
    碼一閱讀 178評論 0 0