0

次のような基本的な Core Data モデルがあります。 ここに画像の説明を入力

データモデルファイルを自動生成したところ、次のようなものが得られました。

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

ここで何が起こっているかを理解している人はいますか?

4

1 に答える 1

1

考えられる理由の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;

多分それは物事を明確にするでしょう...

于 2013-01-26T15:47:58.260 に答える