UISearchBar in a UITableView that uses NSMutableDictionary - is it possible?
Hi everyone,
I'm really stuck on this, finding adding a search bar really complicated. I've created a table based app that uses NSMutableDictionary called 'allThings'. Like so:
Code:
[allThings addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Matthew Johnson", @"name", @"Butterfingers", @"nicknameName", @"matthew_johnson.jpg", @"image", @"Matthew_johnson_thumb.jpg",@"thumb", @"Australia", @"location", @"Johnson", @"family", @"Matthew Johnson was born in Perth. He likes to play basketball and drink Coke.", @"description", @"", @"credit", nil]];
This is then stored in an NSMutableArray called 'data'
Code:
[data addObject:allThings];
I want to be able to search by "name" so I've been following a tutorial, and have the following code:
however, the app crashes when i go to input text in the text box with the error:
Code:
[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x44ec40
If anyone could help me out with this, it would be greatly appreciated! Thanks, I've been stuck on it for a while, and not sure whether it is even possible anymore?
You're not doing yourself any favors with this data structure. It's confusing, and that's the reason you are struggling.
allThings - this cannot be a dictionary as you said it is. It must be an array. You are putting a dictionary into allThings, but dictionaries can't do addObject:, therefore allThings is not a dictionary.
Next, your actual dictionary is created like this:
When you do this, the pattern is object, key, object, key, object, key, etc. So right here you are setting the string object @"Matthew Johnson" for the key @"name".
You then add allThings to another array, data. So at this moment, you have an array containing an array that contains a dictionary. I'm guessing you haven't thought this through very well.
In your search routine, you do this:
Code:
for (NSDictionary *dictionary in data)
...but as we just established, data does not contain dictionaries, it contains an array.
This is confirmed right here, where your error occurs:
You are sending objectForKey to an array object. That doesn't work, thus your crash.
In addition to that, you seem to be thinking that the object stored with the key @"name" is an array. But that is not the case. As previously stated, that object is a string, not an array.
Of course this is all possible (do you really think it isn't?), you just haven't set it up well at all. Your goal is not at all clear, and thus your data structures reflect that confusion. Since you don't really understand what your data is, you can hardly be expected to make this work. So sit back and really think about what your data structure needs to be, then try again.
Thanks for your reply BrianSlick, I made the mistake of following a tutorial without taking the time to fully understand the workings of it all. Your explanations make sense, and I am starting to see the errors I have made in structure. Though, I've utterly confused myself and (I think) seriously overcomplicated things.
I'm afraid I'm stuck with a huge amount of data that I don't know if I can use.
I'll think about it for awhile. When you put it like
Quote:
array containing an array that contains a dictionary
it becomes clear how mixed up I've made things. I'm thinking I should simply have 1 mutable array called 'data' that contains the dictionary...
Hmm, but I currently have over 300 "dictionaries", in this structure: hmm, where to start
Code:
[allThings addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Matthew Johnson", @"name", @"Butterfingers", @"nicknameName", @"matthew_johnson.jpg", @"image", @"Matthew_johnson_thumb.jpg",@"thumb", @"Australia", @"location", @"Johnson", @"family", @"Matthew Johnson was born in Perth. He likes to play basketball and drink Coke.", @"description", @"", @"credit", nil]];
Code:
[allThings addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Jack Johnson", @"name", @"JackyJack", @"nicknameName", @"jack_johnson.jpg", @"image", @"Jack_johnson_thumb.jpg",@"thumb", @"Japan", @"location", @"Johnson", @"family", @"Jack Johnson was born in Paris. He likes eating pork.", @"description", @"", @"credit", nil]];
Hmm, At this point I'm thinking of scrapping this 'search' business and just getting the user to scroll through the table... I need to get my head around things more. My table works, it's dirty code, but it works, the 'search' was an added bonus, but if it means starting from scratch, I just can't fathom it at this stage! Appreciate your advice mate.