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 7840235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:42:41+00:00 2026-06-02T15:42:41+00:00

I have an array with several NSString element in and I also have a

  • 0

I have an array with several NSString element in and I also have a view, where i have a search bar in. I want to begin searching for what the user writes in the search field as a substring for each NSString element. If the NSString element contains the substring, i want to add it to a new array. I have tried to do this with code, and it works okay, but it keep adding objects to, even it have already for them, so does anybody have any ideas why? I have already set the delegate to self.

//.h
@interface class1

@property(strong, nonatomic) NSMutableArray *allExercisesNames;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property(strong, nonatomic) NSMutableArray *foo;

@end

//.m

@implementation class1

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{    
for(int i = 0; i < _allExercisesNames.count;i++)
{
    if([[_allExercisesNames objectAtIndex:i] rangeOfString:searchBar.text].location != NSNotFound)
    {
        if(_foo == nil)
        {
            _foo = [[NSMutableArray alloc]initWithObjects:[_allExercisesNames objectAtIndex:i], nil];
            [self.gridView reloadData];

        }
        else 
        {
            [_foo addObject:[_allExercisesNames objectAtIndex:i]];
            [self.gridView reloadData];
        }
    }

}

if([searchBar.text isEqualToString:@""] || searchBar.text==nil)
    //![_allExercisesNames containsObject:searchBar.text
{
    _foo = nil;
    [self.gridView reloadData];
}
} 
  • 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-02T15:42:43+00:00Added an answer on June 2, 2026 at 3:42 pm

    You are not assigning ‘nil’ to _foo at the top of the function, so it continues adding to the old _foo array every time you type something.

    Rather, you should set _foo to a new array at the top of the function instead, so it is cleared before scanning the _allExercisesNames array.

    Here is a fixed, simplified version that also makes use of ‘for (x in y)’ to make it more concise.

    -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        if([searchBar.text isEqualToString:@""] || searchBar.text==nil)
            _foo = nil;
        else
        {
            _foo = [NSMutableArray new];
            for(NSString *exercise in _allExercisesNames)
                if([exercise rangeOfString:searchBar.text].location != NSNotFound)
                    [_foo addObject:exercise];
        }
    
        [self.gridView reloadData];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two dimensional JSON array where each element contains several attributes. The
I have got an array with several twitter tweets and want to delete all
I have several array to deal with. I need to extract the most duplicate
I have an object whose value may be one of several array types like
I have a mutable array that is retained and storing several objects. At some
I have several 450 element character arrays (storing bitmap data to display on lcd
I have a detail view with several textfields, next to one of them i
I have an array of hex color codes and want to loop through them
I have an array which keeps URL of several files. For example: var files
If I have a method called -uploadToServer:(Object *)objectToUpload, and a mutable array of several

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.