尝试保存核心数据记录后出现错误

尝试保存核心数据记录后出现错误

本文介绍了尝试保存核心数据记录后出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器来添加核心数据记录。核心数据实体名称为FavoriteThings,属性为Thingname。我有一个名为SaveButtonAction的保存按钮动作。当我点击按钮内部时,应该存储在文本字段ToDoTextField中插入的文本,但是应用程序崩溃了,并显示以下日志错误:

  2013-12-09 12:30:07.488最喜欢的事情[1701:a0b] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ entityForName:nil不是合法的NSManagedObjectContext参数,用于搜索实体名称' 

这是该方法的代码

 -(IBAction)SaveButtonAction:(id)sender {
FavoriteThing * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@ FavoriteThing inManagedObjectContext:managedObjectContext];
newEntry.thingName = self.ToDoTextField.text;
NSError *错误;
if(![self.managedObjectContext save:& error])
{
NSLog(@糟糕,无法保存:%@,[错误localizedDescription]);
}

谢谢您的时间。.

解决方案

请检查实体名称,并在YourAppDeleagte.h

$

中执行以下操作b
$ b

  +(YourAppDeleagte *)sharedManagedContext; 

在YourAppDeleagte.m

  +(YourAppDeleagte *)sharedManagedContext {

return(YourAppDeleagte *)[[UIApplication sharedApplication] delegate];
}

在viewController.m中



#import YourAppDelegate.h

  @property(nonatomic,retain)NSmanagedObjectContext * managedObjectContext; 

-(void)viewDidLoad {
[super viewDidLoad];
self.managedObjectContext = [YourAppDelagete shareManagedContext] .managedObjectContext;
}


i have a view controller to add a core data record. The core data entity name is FavoriteThings, the attribute is thingname. I have a save button action called SaveButtonAction. When I tap inside the button, the text inserted in the textfield called ToDoTextField should be stored, but the app crashed showing following log error:

2013-12-09 12:30:07.488 Favorite Things[1701:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'FavoriteThing''

This is the code for the method

- (IBAction)SaveButtonAction:(id)sender {
    FavoriteThing *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:managedObjectContext ];
    newEntry.thingName = self.ToDoTextField.text;
    NSError *error;
    if (![self.managedObjectContext save:&error])
    {
        NSLog(@"Whoops, couldn't save:%@",[error localizedDescription]);
    }

Thank you for your time..

解决方案

Please check the enity name and also do the following

in YourAppDeleagte.h

+(YourAppDeleagte*)sharedManagedContext;

in YourAppDeleagte.m

  +(YourAppDeleagte*)sharedManagedContext{

     return (YourAppDeleagte *)[[UIApplication sharedApplication]delegate];
}

in viewController.m

#import "YourAppDelegate.h"

@property(nonatomic,retain)NSmanagedObjectContext *managedObjectContext;

-(void)viewDidLoad{
   [super viewDidLoad];
   self.managedObjectContext=[YourAppDelagete shareManagedContext].managedObjectContext;
 }

这篇关于尝试保存核心数据记录后出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:49