2011-12-30 206 views
0

我不知道为什么我无法从父对象中引用子对象属性。这里的关系是:从父对象引用嵌套对象

@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 

现在,我尝试使用下面的代码来访问蔬菜类的名称属性:

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

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

您是否正在导入Vegetable和MyVegetableGarden? – 2011-12-30 01:40:33

回答

1

确保你已经#进口的名称属性文件中的Vegetable.h

+0

谢谢克里斯!这是问题!我会在几分钟内接受你的回答。 – azamsharp 2011-12-30 01:43:06

1

我不是su再次,你可以创建一个NSManagedObject这样的:S

docs状态

如果直接实例化管理对象,你必须调用指定的初始化(initWithEntity:insertIntoManagedObjectContext:)。

另见

重要提示:此方法是NSManagedObject指定初始化。您不能简单地通过发送init来初始化管理对象。