[英]Searching through NSMutableArray of objects with NSPredicate
問題解決了,這里是我如何修復它以供將來像我這樣的noobs參考我需要刪除空格: 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
您收到此錯誤是因為船舶不是集合,因此您無法使用包含 - 我認為您需要在您的船舶對象中搜索特定字段,並使用LIKE而不是CONTAINS:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.shipBuilder LIKE[cd] %@",searchText];
使用LIKE,您可以在搜索中使用通配符。 您還可以使用BEGINSWITH或ENDSWITH來搜索單詞的開頭或結尾。
不要在謂詞中使用SELF
,因為那將使用CargoShip的實際實例。 在你的鏈接示例中, SELF
工作正常,因為它是一個NSString
。
例如,您可以執行以下操作:
@"(countryOfBuild contains[cd] %@) OR (shipBuilder contains[cd] %@)"
。 等等
我用:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self.youProperty contains[c] %@", searchString];
searchArrayObjects = [arrayObjects filteredArrayUsingPredicate:resultPredicate];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.