views:

54

answers:

1

My goals is search bar on top my table that searches the track_tiles in Dictionary. This table was built by Parsed data into an NSDictionary. My Dictionary for the table looks like...

tracksDict:  {
    "Cold Calling" =     (
        "<Track: 0x5f39bc0>",
        "<Track: 0x5f3a3e0>",
        "<Track: 0x5f3a990>",
        "<Track: 0x5f3ae80>"
    );
    "Gate Keeper" =     (
        "<Track: 0x5f3b3e0>",
        "<Track: 0x5f3b980>",
        "<Track: 0x5f3bed0>"
    );
    "Hot Calling" =     (
        "<Track: 0x5f3c390>"
    );
}

Below is my attempt of making a searchTable...

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    searchArray = listOfItems;
    NSLog(@"Sorted: %@", searchArray);

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"track_title contains %@", searchText];  
    [searchArray filterUsingPredicate:predicate];  
    [copyListOfItems addObjectsFromArray:searchArray];

    [searchArray release];
    searchArray = nil;
}

I figured out how to use NSPredicate to search Track.track_title. But if I delete my current search (change searchText), then it crashes.

For example, if I search "pro". The new copyListOfItems array is correct. With two items in my array. But if I were to delete the "o" in pro or press "search" it crashes. I need some kind of loop that fix this problem. Please and thank you.

A: 

First, it's unclear exactly what you're looking for, so please think through your question and try to rephrase it more clearly.

Second, it's unclear how your search mechanism is supposed to work. Is copyListOfItems an instance variable (a property of "self" in this context)? How about listOfItems? How are you managing the memory for these properties? Are you properly flagging your UI to update?

Third, it looks like you're missing some basic memory management concepts. You alloc/init a mutable array and assign it to your searchArray, then you point searchArray at some other object called listOfItems, thereby leaking the mutable array you just alloc/init'd in the previous line.

Fourth, you haven't listed the specifics of your crash (like the specific signal and any debugging information). Without this information, it's tough to say.

If I had to guess, I'd say you're over-releasing listOfItems since you point searchArray at it, then release it through [searchArray release]. By the time your UI tries to read it to update your table, it's gone.

Joshua Nozzi
NSMutableArray *searchArray = [[NSMutableArray alloc] initWithArray:listOfItems];
slowman21
Thanks Joshua, you helped me realized the above.
slowman21