ios如何在Core Data中的实体中添加事件?
我有一个实体“用户”和一个属性“名称”。我正在尝试使用 FirstViewController.m 文件中使用的此代码向实体“用户”添加一个名称:
- (IBAction)addUser:(id)sender {
User *user = (User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:managedObjectContext];
[user setName:@"Bhagwan"];
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
}
但它显示错误消息:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIViewController addUser” :]: 无法识别的选择器发送到实例 0x4d38950'
如何解决这个问题? 请帮忙!!
编辑:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import <CoreData/CoreData.h>
@interface CoreDataTry2AppDelegate : NSObject <UIApplicationDelegate> {
UIViewController *_first; } @property (nonatomic, retain) IBOutlet UIViewController *first;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain, readonly) NSManagedObjectContext
*managedObjectContext; @property (nonatomic, retain, readonly) NSManagedObjectModel
*managedObjectModel; @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator
*persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
这是应用程序委托文件。
I have an entity 'USer' and one attribute 'name'. I am trying to add one name to the entity 'User' using this code which is used in FirstViewController.m file:
- (IBAction)addUser:(id)sender {
User *user = (User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:managedObjectContext];
[user setName:@"Bhagwan"];
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
}
But it is showing an error msg:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController addUser:]: unrecognized selector sent to instance 0x4d38950'
how to solve this?
Please help!!
EDIT:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import <CoreData/CoreData.h>
@interface CoreDataTry2AppDelegate : NSObject <UIApplicationDelegate> {
UIViewController *_first; } @property (nonatomic, retain) IBOutlet UIViewController *first;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain, readonly) NSManagedObjectContext
*managedObjectContext; @property (nonatomic, retain, readonly) NSManagedObjectModel
*managedObjectModel; @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator
*persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
This is the App delegate file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题超出了显示的代码范围。您可能会将 addUser: 消息发送到不同的对象(未实现该方法的对象),或者实现该方法的对象已被释放(并且它的内存重用于另一种对象)
您提到的按钮似乎将 UIViewController 设置为目标,并将 addUser: 选择器设置为操作。但是 UIViewController 没有 addUser: 方法,你会得到一个异常。
确保将按钮的操作连接到真正实现 addUser: 方法的对象。
The problem you are experiencing lies outside of the showed code. You are likely sending the addUser: message to a different object (one that does not implement it), or the object that implements the method is already deallocated (and it's memory reused for a different kind of object)
The button you are mentioning seems to have a UIViewController set as it's target and the addUser: selector as action. But UIViewController does not have a addUser: method you get an exception.
Make sure, that you connect the action of the button to an object that really implements an addUser: method.