1

我将 CoreData 与一个实体和该实体的两个属性一起使用。
实体:Binder
属性:名称,lastOpened
,br> 我可以毫无问题地插入实体的新对象,我也可以设置它的名称,但我不能设置它的 lastOpened 属性。
这是我的代码:

Binder *newBinder = [NSEntityDescription insertNewObjectForEntityForName:@"Binder" inManagedObjectContext:context];
[newBinder setName:@"Binder"];
[newBinder setLastOpened:[NSDate date]]; //Tried this first
newBinder.lastOpened = [NSDate date]; //No compiler warning either

但是,当我运行应用程序时,我得到一个错误,-[Binder setLastOpened:]: unrecognized selector sent to instance 0x9688870

我可以验证显示的内存地址实际上是正确的 Binder 对象。关于为什么我可以设置一个属性但不能设置另一个属性的任何想法?谢谢。
活页夹.h:

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

@class Cards;

@interface Binder : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * lastOpened;
@property (nonatomic, retain) NSSet *cards;
@end

@interface Binder (CoreDataGeneratedAccessors)

- (void)addCardsObject:(Cards *)value;
- (void)removeCardsObject:(Cards *)value;
- (void)addCards:(NSSet *)values;
- (void)removeCards:(NSSet *)values;

@end

活页夹.m:

#import "Binder.h"
#import "Cards.h"


@implementation Binder

@dynamic name;
@dynamic lastOpened;
@dynamic cards;

@end
4

3 回答 3

1

Xcode 偶尔会搞砸重建对 xcdatamodel 的更改。下一次,尝试做一个干净的构建。

此外,您不应该硬核类名字符串,因为它会破坏重构。

[NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Binder class]) inManagedObjectContext:context]

此外,为类名和托管对象添加前缀。(例如更喜欢 XYZBinder 而不是 Binder)。您将避免将来因命名空间冲突而感到痛苦。

于 2013-03-29T18:48:27.597 回答
0

您可能已经在项目中加载了另一个名为 Binder 的类。尝试将 CoreData 实体的类名设置为 XCode 中的 BinderMO 之类的名称,然后重新创建类文件。

于 2013-03-29T16:40:10.793 回答
0

我不确定问题出在哪里,但我删除了我的 Binder.h/.m 文件并重新创建了它们,现在它可以工作了。旧的和新的唯一的区别是'name'和'lastOpened'交换了位置。谢谢您的帮助。

新活页夹.h:

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

@class Cards;

@interface Binder : NSManagedObject

@property (nonatomic, retain) NSDate * lastOpened;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *cards;
@end

@interface Binder (CoreDataGeneratedAccessors)

- (void)addCardsObject:(Cards *)value;
- (void)removeCardsObject:(Cards *)value;
- (void)addCards:(NSSet *)values;
- (void)removeCards:(NSSet *)values;

@end

新活页夹.m:

#import "Binder.h"
#import "Cards.h"


@implementation Binder

@dynamic lastOpened;
@dynamic name;
@dynamic cards;

@end
于 2013-03-29T17:02:18.290 回答