简单方法:我有一个SearchView
,用户可以在其中选择一些CoreDate,他可以搜索Name
,City
(NSString
}和{{1} }(Clientnumber
)
NSNumber
这是我对名称和城市的谓词,但是如何在NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"Name contains[cd] %@ OR City contains[cd] %@",
searchText,searchText];
字段中搜索搜索查询?
因此,如果我要搜索 300 ,则应返回NSNumber
:300123和1230012的条目,因为包含 300
那么,我该怎么做呢?有可能"转换" Clientnumber
到Clientnumber
内的NSPredicate
的价值?
答案 0 :(得分:6)
您应该可以使用CAST(Clientnumber, 'NSString') contains[cd] %@
进行搜索:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"Name contains[cd] %@ OR City contains[cd] %@ OR CAST(Clientnumber, 'NSString') contains[cd] %@",
searchText,searchText,searchText];
答案 1 :(得分:0)
我发现在使用NSFetchedResultsController
的核心数据搜索中,dasblineklight的答案对我不起作用。然而,DID的工作是将number.description
作为属性的名称传递,因此对于这种具体情况,将是:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"Name contains[cd] %@ OR City contains[cd] %@ OR Clientnumber.description contains[cd] %@",
searchText, searchText, searchText];