2014-01-21 43 views
2

我有一個核心數據項目,並且難以用簡單的計算字段搜索數據,並且不知道爲什麼它不起作用。NSPredicate不能使用計算字段

我有一個Tutor實體,核心數據字符串屬性爲「tutorFirstName」和「tutorLastName」。我創建了一個附加的字符串屬性「tutorFullName」,這是一類填充這樣:

NSString *fullName = [@[self.tutorFirstName, self.tutorLastName] componentsJoinedByString:@" "]; 

將數據填充,但是當我執行下面的搜索只在tutorFirstName和tutorLastName工作的謂詞:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"tutorFirstName = %@", @"Sean"]; 
[fetchRequest setPredicate:predicate]; 
NSArray *fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error]; 
NSLog(@"Search on %@ returned %lu items (%@)",predicate, (unsigned long)fetchedObjects.count, [fetchedObjects objectAtIndex:0]); 

predicate = [NSPredicate predicateWithFormat:@"tutorFullName = %@", @"Sean Silverman"]; 
[fetchRequest setPredicate:predicate]; 
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error]; 
NSLog(@"Search on %@ returned %lu items",predicate, (unsigned long)fetchedObjects.count); 

predicate = [NSPredicate predicateWithFormat:@"tutorLastName = %@", @"Silverman"]; 
[fetchRequest setPredicate:predicate]; 
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error]; 
NSLog(@"Search on %@ returned %lu items (%@)",predicate, (unsigned long)fetchedObjects.count, [fetchedObjects objectAtIndex:0]); 

這裏是表示與每個搜索結果的調試代碼還使得所得核心數據對象不具有正確的tutorFullName條目。

2014-01-21 14:05:26.129 AT Data[54935:70b] Search on tutorFirstName == "Sean" returned 1 items (<Tutor: 0x113b16830> (entity: Tutor; id: 0xd0000000041c0006 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Tutor/p263> ; data: { 
charges =  (
    "0xd000000581b40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90221>", 
    "0xd00000058b280002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90826>", 
    "0xd00000057d4c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p89939>", 
    "0xd0000005994c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91731>", 
    "0xd000000593c40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91377>", 
    "0xd0000005aa3c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92815>", 
    "0xd0000005ab440002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92881>", 
    "0xd0000005aa500002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92820>", 
    "0xd00000057ea00002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90024>", 
    "0xd0000005a7000002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92608>", 
    "(...and 595 more...)" 
); 
tutorFirstName = Sean; 
tutorFullName = "Sean Silverman"; 
tutorLastName = Silverman; 

2014-01-21 14:05:26.130 AT Data[54935:70b] Search on tutorFullName == "Sean Silverman" returned 0 items 

2014-01-21 14:05:26.131 AT Data[54935:70b] Search on tutorLastName == "Silverman" returned 1 items (<Tutor: 0x113b16830> (entity: Tutor; id: 0xd0000000041c0006 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Tutor/p263> ; data: { 
charges =  (
    "0xd000000581b40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90221>", 
    "0xd00000058b280002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90826>", 
    "0xd00000057d4c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p89939>", 
    "0xd0000005994c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91731>", 
    "0xd000000593c40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91377>", 
    "0xd0000005aa3c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92815>", 
    "0xd0000005ab440002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92881>", 
    "0xd0000005aa500002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92820>", 
    "0xd00000057ea00002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90024>", 
    "0xd0000005a7000002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92608>", 
    "(...and 595 more...)" 
); 
tutorFirstName = Sean; 
tutorFullName = "Sean Silverman"; 
tutorLastName = Silverman; 

,這似乎很奇怪,我的唯一的事情是,在實體的調試輸出,嚴格的核心數據的屬性沒有周圍的字符串值的雙引號,但計算出的屬性呢(」肖恩西爾弗曼「),但這可能只是因爲計算屬性中有空間。

任何幫助將不勝感激。

回答

9

通過計算,我假設您的意思是該屬性是暫時的。如果是這種情況,那麼您無法搜索這些屬性。這是因爲在搜索所有正在搜索的核心數據實體的搜索時,可能不會創建該屬性。從Core Data Programming Guide(剛剛上市1後):

不能提取採用了基於瞬態特性謂詞(雖然你可以使用瞬態特性在記錄自己進行篩選)。

因此,如果您想要使用謂詞來過濾該屬性,則需要將該屬性設置爲非瞬態。

+0

同意。爲了@OP爲什麼不簡單地在名字和姓氏上使用AND運算符? –

+0

我試圖避免使用AND解決方案,因爲我收到了全名,並且會將問題解析爲第一個和最後一個,因爲某些名稱有多個空格(有些用於名字,例如Mary Jane Smith,其中Mary Jane是第一個名字,還有一些用於姓氏,即Mary Smith Perry,其中Smith Perry是姓氏) – user3220687

+0

好的,我切換到AND解決方案併爲我的間距問題創建瞭解決方法。我所做的是通過用#替換所有空格來對名字和姓氏進行編碼,然後在需要打印的值時將其解碼。只要我的名字都沒有#在他們中,我應該沒問題。 – user3220687