The issue is resolved, this is how I fixed it for future reference for noobs like me
I needed to remove the white spaces:
ship.countryOfbuild = [[elements objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
And I had to change the predicate as follows: NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(countryOfBuild contains[cd] %@) OR (shipBuilder contains[cd] %@) OR (name contains[cd] %@) OR (owner contains[cd] %@)",searchText, searchText, searchText, searchText];
Original question
I use this tutorial to add a search bar to my project. I got tableview everything works, but the search function crashes the application. Now I am looking for:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [searchContent filteredArrayUsingPredicate:resultPredicate];
}
searchContent- A mutable array that is read from. CSV file as follows:
- (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];
}
I would like the search to search all 15 parts of the object (name, size, ship class, etc.)
This is currently happening with an error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection <CargoShips: 0x714e1b0> (not a collection)'
CargoShips is my NSObject class.
How to fix NSPredicate to search through NSMutableArraywith objects without crashing?
, http://pastebin.com/sw12GwHK