创建User对象而不是NSEntityDescription对象

时间:2015-03-14 10:56:21

标签: ios objective-c

我是Objective-C的新手。我试图从SingupViewController发送一个User类型的对象(由核心数据制作)到它的委托ViewController。但是我得到了你从我读过的内容中得到的这个错误,因为我为同一个对象重复实例化内存? 。错误如下:

[User managedObjectModel]: unrecognized selector sent to instance 0x10ca90560
2015-03-14 03:44:49.613 Honk2[302:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[User managedObjectModel]: unrecognized selector sent to instance 0x10ca90560'

SingupViewController.h的代码是:

#import <UIKit/UIKit.h>
#import "User.h"


@protocol SingupDelegate <NSObject>

- (void)signUp;
- (void)cancelPressDel;
- (void)savePress:(User *)addUser;

@end

@interface SingupViewController : UIViewController

@property (nonatomic, weak) id<SingupDelegate> delegate;

@end

SingupViewController.m的代码是:

import "SingupViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "LoginViewController.h"
#import "ViewController.h"
#import "CoreViewController.h"

@interface SingupViewController () <UITextFieldDelegate>
{

    UILabel *titleApp;

    UITextField *username;
    UITextField *password;
    UITextField *email;
    UIButton *signUp;
    UIButton *cancel;
}
    @property(strong,nonatomic)User *thenewUser;

@end

@implementation SingupViewController
-(void)onSignUpPressed{

    _thenewUser.name = username.text;
    _thenewUser.email = email.text;
    _thenewUser.password = password.text;

    if([[self delegate] respondsToSelector:@selector(savePress:)]) {
        [[self delegate] savePress:_thenewUser];

    }

}

@end

ViewController.h的代码是:

#import <UIKit/UIKit.h>
#import "CoreViewController.h"
#import "User.h"

@interface ViewController : CoreViewController <NSFetchedResultsControllerDelegate>

@end

ViewController.m的代码是:

#import "ViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "LoginViewController.h"
#import "SingupViewController.h"
#import "AppDelegate.h"
#import "CoreViewController.h"


@interface ViewController () <LoginDelegate, SingupDelegate>
{
    LoginViewController *login;
    SingupViewController *singup;
}
    @property (nonatomic, strong) NSManagedObjectContext *managedObject;
    @property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;

@end

@implementation ViewController
-(void)savePress:(User *)addUser{

    NSEntityDescription *description = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[self managedObjectContext]];

    NSManagedObject *newUser = [[NSManagedObject alloc] initWithEntity:description insertIntoManagedObjectContext:[self managedObjectContext]];

    [newUser setValue:addUser.name forKey:@"name"];
    [newUser setValue:addUser.email forKey:@"email"];
    [newUser setValue:addUser.password forKey:@"password"];

    NSError *error = nil;

    if(![newUser.managedObjectContext save:&error]){
        NSLog(@"invatam si noi sa codam?");
    }
    else{
        NSLog(@"Hai ca am invatam sa codat");
    }


    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideTopBottom];
}
-(NSManagedObjectContext*)managedObjectContext{
    return[(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}

User.h是Xcode给出的:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Tweet;

@interface User : NSManagedObject

@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSSet *tweets;
@end

@interface User (CoreDataGeneratedAccessors)

- (void)addTweetsObject:(Tweet *)value;
- (void)removeTweetsObject:(Tweet *)value;
- (void)addTweets:(NSSet *)values;
- (void)removeTweets:(NSSet *)values;

@end

这也是User.m也没有触及:

#import "User.h"
#import "Tweet.h"


@implementation User

@dynamic email;
@dynamic name;
@dynamic password;
@dynamic tweets;

@end

当我尝试保存对象时发生错误。任何人都可以告诉我,我分配错了什么?或者是什么错误?

1 个答案:

答案 0 :(得分:1)

该错误是因为您向managedObjectModel对象发送了User,而User无法对此进行回复。

当实例化NSEntityDescription时,您应该使用以下代码:

[NSEntityDescription entityForName:@"User" inManagedObjectContext:context];

您正在做的是实例化User对象而不是NSEntityDescription对象。然后这一行:

NSManagedObject *newUser = [[NSManagedObject alloc] initWithEntity:description insertIntoManagedObjectContext:[self managedObjectContext]];

description实际上是User个对象而不是NSEntityDescription个对象。

编辑几点:

1)在onSignUpPressed中,如果可能,您应该创建一个新用户并将其添加到上下文中然后如果您真的想要调用savePress:执行此操作:

-(void)onSignUpPressed{

    if([[self delegate] respondsToSelector:@selector(savePress:)]) {
        [[self delegate] savePress:@{@"UName":username.text, @"Email":email.text, @"PWord":password.text}];

    }
}

您在这里所做的是向代表发送包含所有适当值的字典。

然后在savePress:

-(void)savePress:(NSDictionary *)dict{

    NSEntityDescription *description = [NSEntityDescription entityForName:@"User" inManagedObjectContext:[self managedObjectContext]];

    NSManagedObject *newUser = [[NSManagedObject alloc] initWithEntity:description insertIntoManagedObjectContext:[self managedObjectContext]];

    [newUser setValue:dict[@"UName"] forKey:@"name"];
    [newUser setValue:dict[@"Email"] forKey:@"email"];
    [newUser setValue:dict[@"PWord"] forKey:@"password"];

    NSError *error = nil;

    if(![newUser.managedObjectContext save:&error]){
        NSLog(@"invatam si noi sa codam? Error: %@", error.localizedDescription);
    }
    else{
        NSLog(@"Hai ca am invatam sa codat");
    }


    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideTopBottom];
}

当你使用CoreData时,你真的需要有一个计划好的结构,并坚持下去。