从父对象引用嵌套对象

时间:2011-12-30 01:37:04

标签: objective-c ios

我不确定为什么我无法从父级引用子对象属性。这是关系:

@class Vegetable;

@interface MyVegetableGarden : NSManagedObject

@property (nonatomic) NSTimeInterval datecreated;
@property (nonatomic) NSTimeInterval datemodified;
@property (nonatomic) BOOL active;
@property (nonatomic, retain) Vegetable *vegetable;

@end

这是蔬菜类:

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

@class VegetableCategory;

@interface Vegetable : NSManagedObject

@property (nonatomic, retain) NSString * about;
@property (nonatomic) BOOL active;
@property (nonatomic) NSTimeInterval dateCreated;
@property (nonatomic) NSTimeInterval dateModified;
@property (nonatomic, retain) NSString * imageURL;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) VegetableCategory *vegetableCategory;

@end

现在,我尝试使用以下代码访问Vegetable类的name属性:

MyVegetableGarden *v = [[MyVegetableGarden alloc] init];

v.vegetable.name   // For some reason the compiler says that name property is not found

2 个答案:

答案 0 :(得分:1)

确保您在访问名称属性

的文件中#imported Vegetable.h

答案 1 :(得分:1)

我不确定你是否可以像这样创建一个NSManagedObject:S

docs

  

如果直接实例化托管对象,则必须调用指定的初始值设定项(initWithEntity:insertIntoManagedObjectContext:)。

另见

  

重要说明:此方法是NSManagedObject的指定初始值设定项。您不能仅通过发送init来初始化托管对象。