问题已经解决,以下是我如何修复它以供将来参考像我这样的新手
我需要删除白色空格:
ship.countryOfbuild = [[elements objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
我必须像这样更改谓词:NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(countryOfBuild contains[cd] %@) OR (shipBuilder contains[cd] %@) OR (name contains[cd] %@) OR (owner contains[cd] %@)",searchText, searchText, searchText, searchText];
原始问题
我正在使用this tutorial向我的项目添加搜索栏。我让tableview全部工作,但搜索功能正在崩溃应用程序。我现在搜索的方式是:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [searchContent filteredArrayUsingPredicate:resultPredicate];
}
searchContent
是从.csv文件读取的可变数组,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"ships" ofType: @"csv"];
NSString *file = [[NSString alloc] initWithContentsOfFile:pathToFile encoding: NSUTF8StringEncoding error:NULL];
NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
searchContent = [[NSMutableArray alloc] init];
for (NSString* line in allLines) {
NSArray *elements = [line componentsSeparatedByString:@";"];
CargoShips* ship = [[CargoShips alloc]init];
ship.countryOfbuild = [elements objectAtIndex:0];
ship.shipBuilder = [elements objectAtIndex:1];
ship.hullHashtag = [elements objectAtIndex:2];
ship.name = [elements objectAtIndex:3];
ship.owner = [elements objectAtIndex:4];
ship.shipOperator = [elements objectAtIndex:5];
ship.shipDelivery = [elements objectAtIndex:6];
ship.shipFlag = [elements objectAtIndex:7];
ship.shipClass = [elements objectAtIndex:8];
ship.powerPlant = [elements objectAtIndex:9];
ship.HP = [elements objectAtIndex:10];
ship.speed = [elements objectAtIndex:11];
ship.cargoSystem = [elements objectAtIndex:12];
ship.numberOfTanks = [elements objectAtIndex:13];
ship.size = [elements objectAtIndex:14];
[self.searchContent addObject:ship];
}
我希望搜索搜索所有15个对象(名称,大小,船级等)
目前,这会因错误而崩溃:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection <CargoShips: 0x714e1b0> (not a collection)'
CargoShips是我的NSObject类。
如何修复NSPredicate以使用对象搜索NSMutableArray
而不会崩溃?
以下是完整代码,如果这样可以让事情更轻松http://pastebin.com/sw12GwHK
答案 0 :(得分:6)
您收到此错误是因为船舶不是集合,因此您无法使用包含 - 我认为您需要在您的船舶对象中搜索特定字段,并使用LIKE而不是CONTAINS:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.shipBuilder LIKE[cd] %@",searchText];
使用LIKE,您可以在搜索中使用通配符。您也可以使用BEGINSWITH或ENDSWITH来搜索单词的开头或结尾。
答案 1 :(得分:4)
不要在谓词中使用SELF
,因为那将使用CargoShip的实际实例。在您的关联示例中SELF
效果很好,因为它是NSString
。
例如,您可以执行以下操作:
@"(countryOfBuild contains[cd] %@) OR (shipBuilder contains[cd] %@)"
。等等。
答案 2 :(得分:3)
我用:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self.youProperty contains[c] %@", searchString];
searchArrayObjects = [arrayObjects filteredArrayUsingPredicate:resultPredicate];