ios如何在Core Data中的实体中添加事件?

发布于 2024-11-18 19:59:58 字数 1299 浏览 2 评论 0原文

我有一个实体“用户”和一个属性“名称”。我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-11-25 19:59:58

您遇到的问题超出了显示的代码范围。您可能会将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文