5

问题已解决,这是我如何修复它以供像我这样的新手参考, 我需要删除空格: 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];

原始问题

我正在使用本教程向我的项目添加搜索栏。我让 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

4

3 回答 3

6

您收到此错误是因为船舶不是集合,因此您不能使用包含——我认为您需要在船舶对象中搜索特定字段,并使用 LIKE not CONTAINS:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.shipBuilder LIKE[cd] %@",searchText];

使用 LIKE,您可以在搜索中使用通配符。您还可以使用 BEGINSWITH 或 ENDSWITH 来搜索单词的开头或结尾。

于 2013-01-31T16:06:07.977 回答
4

不要SELF在您的谓词中使用,因为这将使用 CargoShip 的实际实例。在您的链接示例SELF中效果很好,因为它是一个NSString.

例如,您可以执行以下操作:

@"(countryOfBuild contains[cd] %@) OR (shipBuilder contains[cd] %@)". 等等等等

于 2013-01-31T15:53:29.560 回答
3

我用:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self.youProperty contains[c] %@", searchString];
searchArrayObjects = [arrayObjects filteredArrayUsingPredicate:resultPredicate];

文档链接

于 2014-02-05T15:35:51.913 回答