0

I'm trying to configure my search bar in my app. I have an array with NSObject instances, and trying to use search option to find decent properties (of this NSObjects). Actually, i can find it. My problem is, i can do it only typing correct letters in UISearch field. When i try to type letter, that "name" property of my NSObject class did not contain, i get an error : index 0 beyond bounds for empty array. I perfectly understand whats going on - array is empty and i try to put out something from an empty array. But i already did check, if my array is empty, then why its error still occur? Please, take a look at code:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    self.searchResults = [NSArray new];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    self.searchResults = [self.placeOfObjects filteredArrayUsingPredicate:resultPredicate];
    if ([self.searchResults objectAtIndex:0] != nil){
    _filteredName = ((placeHolder *)(self.searchResults[0])).name;
    }
    NSLog(@"name is %@", _filteredName );
}

self.placeOfObjects is an array that hold instances of NSObject class "place". It have several properties, name is one among them. It contain only text. How could i implement search without getting an error? Why it still trying to search in empty array when i type ([self.searchResults objectAtIndex:0] != nil)?

Any advice would be appreciated, thanks!

3 Answers 3

2

Why don't you try checking the array count first. If count is greater than 0, then you can go and fetch something from that, else you can just leave it. Try something like this :

if(!(yourMainArray.count==0))
    {
        NSArray *subArray= [yourMainArrayobjectAtIndex:0];
    }
else
   {
        //Show some alert regarding empty data !
   }

Your placeOfObjects will be your mainArray here. And after that you are filling up your SearchResults array from it. So check the count of placeOfObjects Count and only fill the other array if it has count > 0.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

In case of empty array if you will try to access its value placed at any index, it'll be an error

change the condition to

 if([self.searchResults count]) 
 {
     // code 
 }

OR use firstObject method. If the array is empty, it'll returns nil.

 if([self.searchResults firstObject]) 
 {
     // code 
 }

Comments

1

If you don't care which object you get, NSArray has a methods called lastObject. It obviously returns the last object of your array but in case of an empty array it will return nil and won't crash.

Unfortunately there is no equivalent method called firstObject.

EDIT

Apple added firstObjectwith iOS 7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.