iOS - 核心数据自动生成模型 - 无法识别的选择器

时间:2013-01-26 15:42:09

标签: ios core-data selector nsmanagedobject

我有一个基本的Core Data模型,如下所示: enter image description here

我自动生成了数据模型文件,它给了我类似的东西:

BSStudent.h

//
//  BSStudent.h
//

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

@class BSFormation;

@interface BSStudent : NSManagedObject

@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * lastname;
@property (nonatomic, retain) NSDate * birthdate;
@property (nonatomic, retain) id thumbnail;
@property (nonatomic, retain) NSSet *formations;
@end

@interface BSStudent (CoreDataGeneratedAccessors)

- (void)addFormationsObject:(BSFormation *)value;
- (void)removeFormationsObject:(BSFormation *)value;
- (void)addFormations:(NSSet *)values;
- (void)removeFormations:(NSSet *)values;

@end

BSStudent.m

//
//  BSStudent.m
//

#import "BSStudent.h"
#import "BSFormation.h"


@implementation BSStudent

@dynamic firstname;
@dynamic lastname;
@dynamic birthdate;
@dynamic thumbnail;
@dynamic formations;

@end

我尝试通过执行以下操作简单地保存BSStudent对象:

BSStudent *newStudent = (BSStudent *)[NSEntityDescription entityForName:kBSStudent inManagedObjectContext:self.managedObjectContext];
    newStudent.firstname = firstname;
    newStudent.lastname = lastname;
    newStudent.birthdate = birthdate;
    newStudent.thumbnail = thumbnail;

    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Error: %@", [error localizedDescription]);
    }

但我的应用总是崩溃,错误:[NSEntityDescription setFirstname:]: unrecognized selector sent to instance 0x1f021e00

有人在弄清楚这里发生了什么?

1 个答案:

答案 0 :(得分:1)

一种可能的解释是您使用了错误的字符串:

BSStudent *newStudent = (BSStudent *)[NSEntityDescription entityForName:kBSStudent inManagedObjectContext:self.managedObjectContext];

尝试检查newStudent实际是什么:

BSStudent *newStudent = (BSStudent *)[NSEntityDescription entityForName:kBSStudent inManagedObjectContext:self.managedObjectContext];
NSLog(@"New Student: %@", [newStudent description]);
newStudent.firstname = firstname;

也许它会澄清事情......