Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2011, 10:08 AM   #1 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Question 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:

Code:
- (void) searchTableView {
	
	NSString *searchText = searchBar.text;
	
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];
	
	for (NSDictionary *dictionary in data)
	{

            NSArray *array = [dictionary objectForKey:@"name"];
        
		[searchArray addObjectsFromArray:array];
	}
	
	for (NSString *sTemp in searchArray)
	{
		NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
		
		if (titleResultsRange.length > 0)
			[copyListofItems addObject:sTemp];
	}
	
	[searchArray release];
	searchArray = nil;
}

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?

Matt
__________________
Please visit - MushroomPudding.com
marshusensei is offline   Reply With Quote
Old 11-21-2011, 10:25 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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:

Code:
...initWithObjectsAndKeys:@"Matthew Johnson", @"name"
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:

Code:
NSArray *array = [dictionary objectForKey:@"name"];

[__NSArrayM objectForKey:]: unrecognized selector
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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-21-2011, 09:14 PM   #3 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Thanks -

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]];
Thanks again for your critique Brian.
__________________
Please visit - MushroomPudding.com
marshusensei is offline   Reply With Quote
Old 11-21-2011, 09:16 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

1. Those dictionaries are being leaked.

2. I wouldn't use dictionaries. Make a model object that has all of those properties. Much easier.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-21-2011, 09:28 PM   #5 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Thanks

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.

Regards

Matt
__________________
Please visit - MushroomPudding.com
marshusensei is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutabledictionary, searchbar, tableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 392
14 members and 378 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:33 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0