Search code examples
objective-cnsstringnsmutablearrayuisearchbarstoring-data

NSMutableArray of nsstrings (in one of the UISearchBar delegates) possible with persistent storage?


I would like to make a history list in my app. I have surfed and looked at many questions here, but so far I haven´t found the answer to my question.

My codes:

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog (@"search bar text did end editing!");
    NSString *bookmarked = mySearchBar.text;    
    NSMutableArray *bookmarkl = [[NSMutableArray alloc] init];
    int i;
    for (i = 0; i < [bookmarkl count]; i ++)
    {
        [bookmarkl addObject:bookmarked];
    }
    for (NSString *sa in bookmarkl);
    NSLog (@"whole array: %@", bookmarkl);
}

But all I get is one string at a time. What is wrong? I want to add a new string to the mutable array, how should I do this?


Solution

  • You are creating a new list every time the function is called. You should add bookmarkl to your class as a property and only create it once during initialization. If you want the history to persist when your app is closed you will also want to write the data to a file. You can use [NSMutableArray arrayWithContentsOfFile] to load the history and writeToFile to save it.