我一直在研究一个应用程序,该应用程序需要关于情绪的用户评级。我正在使用Core Data来存储这些数据。最初,我试图存储评分和“成就”字符串。我已经在属性Date(Date类型),dailyRating(类型为Int16),dailyAccomp(类型为String)和dailyAccompRating(类型为Int16)的Core Data中创建了一个名为“Day”的实体。我的应用程序委托的核心数据persistentStoreCoordinator方法在我的应用程序崩溃在以下stmnt:核心数据持久性商店协调员NSURL错误
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SelfEsteemBldr.sqlite"];
是我给出的
错误[NSPathStore2 URLByAppendingPathComponent:]:无法识别的选择发送到实例0x4d6f440。
也许有关错误发生的一些背景可能会有所帮助。
我的主窗口有一个标签栏控制器作为rootViewController。在CD模型(LogViewController)的选项卡中,我在导航控制器中设置了一个tableView控制器。导航栏有一个添加按钮,它推动一个基本上具有文本框的新视图,以便用户输入相关数据。在该视图中,有一个带有完成按钮的导航栏。用户完成后,完成按钮变为保存按钮。当我点击保存按钮时,应用程序崩溃。保存按钮是ViewDidLoad中的UIButtonItem。下面是保存按钮的代码:我注释掉的大部分代码,试图找到
- (void) performAddNewDay:(id)paramSender{
SelfEsteemBldrAppDelegate *delegate = (SelfEsteemBldrAppDelegate *)
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;
NSLog(@"Past init point in PerformAddNewDay");
// Get the values from the text fields
NSInteger dailyRatingAsInteger = [self.textFieldDailyRating.text integerValue];
NSNumber *ddailyRating = [NSNumber numberWithInteger:dailyRatingAsInteger];
NSLog(@"Daily Rating Entered is %@", ddailyRating);
NSString *ddailyAccomplishment = self.textFieldAccomplishment.text;
NSLog(@"Daily Accomplishment Entered is %@", ddailyAccomplishment);
NSInteger dailyAccompRatingAsInteger = [self.textFieldAccomplishmentRating.text integerValue];
NSNumber *ddailyAccompRating = [NSNumber numberWithInteger:dailyAccompRatingAsInteger];
NSLog(@"Daily Accomp Rating Entered is %@", ddailyAccompRating);
// Create a new instance of Day
Day *newDay = [NSEntityDescription
insertNewObjectForEntityForName:@"Day"
inManagedObjectContext:context];
if (newDay != nil){
// Set the properties according to the values we retrieved from the text fields
newDay.dailyAccomp = ddailyAccomplishment;
newDay.dailyRating = ddailyRating;
newDay.dailyAccompRating = ddailyAccompRating;
NSError *savingError = nil;
// Save the new day
if ([context save:&savingError] == YES){
// If successful, simply go back to the previous screen
[self.navigationController popViewControllerAnimated:YES];
} else {
// If we failed to save, display a message
[self
displayAlertWithTitle:NSLocalizedString(@"Saving", nil)
message:NSLocalizedString(@"Failed to save the context", nil)];
}
} else {
// We could not insert a new Day managed object
[self
displayAlertWithTitle:NSLocalizedString(@"New Day", nil)
message:NSLocalizedString(@"Failed To Insert A New Day", nil)];
}
}
:
UIBarButtonItem *newSaveButton =
[[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Save", nil)
style:UIBarButtonItemStylePlain
target:self
action:@selector(performAddNewDay:)];
self.saveButton = newSaveButton;
[newSaveButton release];
的UIButtonItem内performAddNewDay方法看起来是这样的并且它似乎是
**NSManagedObjectContext *context = delegate.managedObjectContext;**
也就是说,如果我评论每一个下面包括这个stmnt,应用程序不会崩溃。它什么都不做,只是“等待”(如预期)。如果我取消注释这个stmnt,应用程序崩溃。 SelfEsteemBldrAppDelegate使用#进口“SelfEsteemBldrAppDelegate.h” stmnt还进口
再次,我得到的错误是在中SelfEsteemBldrAppDelegate.m核心数据堆栈persistentStoreCoordinator方法发生在崩溃之后:。
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SelfEsteemBldr.sqlite"];
我正在给定的错误是
[NSPathStore2 URLByAppendingPathComponent:]:无法识别的选择发送到实例0x4d6f440
所以,毕竟,为什么我可能会得到这个消息,以及我能做些什么来解决它?据我所知,我不应该与Core Data Stack方法交互,所以我不想与这些代码混在一起。任何帮助将不胜感激。另外,如果我遗漏了您可能需要的任何信息,请告诉我。谢谢。
愚蠢的问题,但这会去哪里?在应用程序委托PSC方法或PerformAddNewDay方法? – pina0004
您尝试在'performAddNewDay'处保留断点,您将遇到断点将被触发,因此您必须将此代码保存在performAddNewDay中...只是尽量避免将初始程序更改为appdelegate – DShah
好的,非常感谢!我很感激。我尝试使用以下内容: SelfEsteemBldrAppDelegate * delegate =(SelfEsteemBldrAppDelegate *) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext * context = delegate.managedObjectContext; if(context == nil){context = [(SelfEsteemBldrAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; } 我也尝试过在“上下文”消息之前,但同样的事情。 任何见解?我想弄明白这一点并理解它。 – pina0004