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

标签 ios core-data selector nsmanagedobject

我有一个基本的核心数据模型,如下所示: 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:]: 无法识别的选择器发送到实例 0x1f021e00

有人知道这里发生了什么吗?

最佳答案

一种可能的解释是您在以下位置使用了错误的字符串:

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;

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

关于iOS - 核心数据自动生成模型 - 无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14538490/

相关文章:

ios - 核心数据 - 无法加载数据,找不到实体

Jquery 按名称隐藏作为选择器

ios - 点击时更改 UICollectionViewCell 上的 ImageView 图像

iOS Swift Twilio 可编程聊天推送通知

ios - 将逻辑放入 CoreData 模型中

iphone - 存储多个 View Controller 共有的数据

jquery - 使用 jQuery 检查 id 和 class

css - 我如何使用 css 选择器只为两个相同类型的 div 之间的第一个 div 提供样式?

objective-c - setContentOffset:animated:之后滚动被禁用,我在做什么错?

ios - iOS:如何避免在搜索栏中输入特殊字符?