2011-06-05 69 views
0

我想獲取以給定字符串開頭的所有項目。
下面的代碼返回數千個項目,當我做不是使用謂詞,但是當它存在時,我得到零記錄返回。

我已經測試過的字段名稱和術語確實存在於sqlite數據庫中。獲取以給定字符串開頭的所有項目

NSManagedObjectContext *context  = [[DataSource sharedInstance] managedObjectContext]; 
NSEntityDescription *entityDesc  = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context]; 
NSSortDescriptor  *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:YES]; 
NSPredicate   *predicate  = [NSPredicate predicateWithFormat:@"(%@ beginswith %@)", fieldName, searchTerm, nil]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

[request setEntity:entityDesc]; 
[request setPredicate:predicate]; 
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

NSError *error = nil; 
NSArray *objects = [context executeFetchRequest:request error:&error]; 
[request release]; 

return objects; 

解決
下解決了這個問題:是有錯誤的報價包裝紙

NSString    *str   = [NSString stringWithFormat:@"%@ beginswith \"%@\"", fieldName, titleTerm]; 
NSPredicate   *predicate  = [NSPredicate predicateWithFormat:str]; 

內容。

回答

1

解決自己的問題時,應該回答自己的問題,而不是編輯問題。如果你不這個問題將顯示爲未答覆。你可以繼續並接受這個答案,問題將被正確標記爲已解決。


下解決了這個問題:是有錯誤的報價包裝紙

NSString    *str   = [NSString stringWithFormat:@"%@ beginswith \"%@\"", fieldName, titleTerm]; 
NSPredicate   *predicate  = [NSPredicate predicateWithFormat:str]; 

內容。

相關問題