Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:36:19+00:00 2026-06-18T09:36:19+00:00

The question is solved, here is how I fixed it for future reference for

  • 0

The question is solved, here 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 like this: 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’m using this tutorial to add a search bar to my project. I got the tableview all working, but the search function is crashing the app. The way I search now is:

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

searchResults = [searchContent filteredArrayUsingPredicate:resultPredicate];
}

The searchContentis a mutable array read from a .csv-file like this:

- (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 through all 15 pieces of the object (name, size, shipclass etc)

Currently, this crashes with the 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 do I fix the NSPredicate to search through an NSMutableArraywith objects without crashing?

Here is the full code if that makes stuff easier http://pastebin.com/sw12GwHK

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:36:20+00:00Added an answer on June 18, 2026 at 9:36 am

    You get this error because a ship is not a collection, so you can’t use contains — I think you need to search for a particular field in your ship object, and use LIKE not CONTAINS:

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

    With LIKE you can use wild cards in the search. You could also use BEGINSWITH or ENDSWITH to search for the beginning or end of words.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have asked a question about manytomanyfield here which i solved but now a
I've looked around here for similar question but couldn't find any that solved my
I've asked a similar question before here and the answer solved my problem. However,
I found a similar question/answer here: How to render a table with some fixed
Here is a n00b question, but one I can't seem to solve reading my
I'm trying to solve interviewstreet's median challenge. I saw a similar question posted here:
I know there's like 3-5 similar questions here, but non of the answers solves
edit This question is solved! Having something weird. I'm using html { font-size: 100%
After my last question was solved the JSON I'm receiving from the server changed
Edit 4/4/12 I STILL HAVE ONE QUESTION: I solved my issue but it adds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.