2011-02-10 17 views
1

我有一個像實體中的以下內容:是否可以在NSFetchRequest的NSPredicate中使用父實體的屬性?

@interface A : NSManagedObject 
{ 
} 

@property (nonatomic, retain) NSString *stringProperty; 

具有子實體b類似這樣:

@interface B : A 
{ 
} 

我想利用存儲在A.事物的性質上B中取像這樣:

NSManagedObjectContext *context = [delegate mainManagedObjectContext]; 
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"B"  inManagedObjectContext:context]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 
NSPredicate *pred = [NSPredicate predicateWithFormat:@"stringProperty = %@", someString]; 
[request setPredicate:pred]; 

這可能嗎?我目前收到以下錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath stringProperty not found in entity <NSSQLEntity B id=26>' 
+0

你能描述你的實體層次多一點,並顯示你正在使用的實際謂詞嗎? – 2011-02-23 20:04:07

+0

我已更新我的問題,更具體的代碼片段。 – cmour 2011-02-24 22:40:38

回答

0

我剛剛在最新的SDK(4.3)中嘗試了一個類似的例子,它現在可以工作。我現在可以在子實體的謂詞中使用父實體的屬性。

0

需要撥打superentity嗎?即

[NSPredicate predicateWithFormat:@"superentity.stringProperty = %@", someString]; 

或者您可以爲您的獲取請求獲取實體A和setIncludesSubentitles:YES

相關問題