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 09-13-2010, 01:54 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 6
bruckerrlb is on a distinguished road
Default Create array of objects from dictionary based on key

Hello all. I have a question have been working on this for a while and I think there might be a simple solution but cannot seem to find it anywhere.

I have a tableview with a search bar. The data for the table view is called from a sqlite db in it's own class, gets saved to the app delegate and is then brought into the view controller. Really I have no problem getting the data back and forth with search. The problem is with didSelectRowAtIndexPath, I"m trying to pull up data for a specific row, for example nameID and nameText. I'm trying to store this key-value info in an NSDictionary so that when a specific name is searched (name), it's nameID (key) stays with it, which I can then use to pull up specific data for the next table view. Well, regardless to say it's not working.

I get my original data from the app delegate
Code:
	NSArray *lastNames = [NSArray arrayWithArray:appDeleg.lastNameSearch];
	NSArray *contactID = [NSArray arrayWithArray:appDeleg.contactID];
Then I put it in a dictionary
Code:
NSDictionary *lastNameDictionary = [NSDictionary dictionaryWithObjects:lastNames, contactID forKeys:@"lastNameKey", @"contactID"];
When I do a nslog of the lastNameDictionary, it shows me exactly what I need, a list of last names and there ID's, so really it's like
44535 = name1
44536 = name2
44537 = name3
etc.

Now the problem I'm having is extracting data from the dictionary. If I do a simple NSLog, everything shows up fine, but when I try to put the values for the lastNameKey key, I get null
Code:
	NSLog(@"Spit out array is %@", lastNameDictionary); //shows me everything I need
	NSArray *tempLastName = [lastNameDictionary valueForKey:@"lastNameKey"];
	NSLog(@"tempLastName array is %@", tempLastName); //comes up Null every time
Can someone assist me in storing my specific dictionary key value in an array
bruckerrlb is offline   Reply With Quote
Old 09-13-2010, 08:12 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by bruckerrlb View Post
Hello all. I have a question have been working on this for a while and I think there might be a simple solution but cannot seem to find it anywhere.

I have a tableview with a search bar. The data for the table view is called from a sqlite db in it's own class, gets saved to the app delegate and is then brought into the view controller. Really I have no problem getting the data back and forth with search. The problem is with didSelectRowAtIndexPath, I"m trying to pull up data for a specific row, for example nameID and nameText. I'm trying to store this key-value info in an NSDictionary so that when a specific name is searched (name), it's nameID (key) stays with it, which I can then use to pull up specific data for the next table view. Well, regardless to say it's not working.

I get my original data from the app delegate
Code:
	NSArray *lastNames = [NSArray arrayWithArray:appDeleg.lastNameSearch];
	NSArray *contactID = [NSArray arrayWithArray:appDeleg.contactID];
Then I put it in a dictionary
Code:
NSDictionary *lastNameDictionary = [NSDictionary dictionaryWithObjects:lastNames, contactID forKeys:@"lastNameKey", @"contactID"];
When I do a nslog of the lastNameDictionary, it shows me exactly what I need, a list of last names and there ID's, so really it's like
44535 = name1
44536 = name2
44537 = name3
etc.

Now the problem I'm having is extracting data from the dictionary. If I do a simple NSLog, everything shows up fine, but when I try to put the values for the lastNameKey key, I get null
Code:
	NSLog(@"Spit out array is %@", lastNameDictionary); //shows me everything I need
	NSArray *tempLastName = [lastNameDictionary valueForKey:@"lastNameKey"];
	NSLog(@"tempLastName array is %@", tempLastName); //comes up Null every time
Can someone assist me in storing my specific dictionary key value in an array
It seems to me that if you're trying to set up a data source for a table view, you want an array of dictionaries, not a dictionary of arrays.

Imagine that each of my people has a lastName and a contactID.

For each person, I would say

Code:
NSDictionary* firstPerson = [NSDictionary dictionaryWithObjectsAndKeys:
  @"Smith",    kLastNameKey,
  [NSNumber numberWithInt: 1234] kContactIDKey,
  nil];


NSDictionary* secondPerson = [NSDictionary dictionaryWithObjectsAndKeys:
  @"Jones",    kLastNameKey,
  [NSNumber numberWithInt: 456] kContactIDKey,
  nil];
Then I would create an array of these dictionaries:

Code:
NSArray* peopleArray = [NSArray arrayWithObjects: firstPerson, secondPerson, nil];
Now we have an array where each element in the array is all the data pertaining to a person. That data is in the form of a dictionary.

(You could also set up a custom data container object and call it something like personRecord. then you could use property notation to get and set the values from the personRecord. That's a horse of a different color however...)

Obviously, you would want code that set up your arrray in a loop, by reading data from a file, off a network, or something.

Anyway, once you have an array of dictionaries, you would do something like this:

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
{
  UITableViewCell* cell;
  //re-use or create a cell 
  cell.text = [[peopleArray objectAtIndex: indexPath.row] objectForKey: kLastNameKey];
}
That code would set up the person's last name as the cell text. You would no doubt want something different, but it illustrates the concept.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-13-2010, 08:27 PM   #3 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Well, if your dictionary is like
44535 = name1
44536 = name2
44537 = name3
etc.

Then it doesn't contain the key "lastNameKey". Instead it has keys 44535, 44536, 44537, etc. If you use them as keys, you should get something, but not "lastNameKey".
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-14-2010, 09:56 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 6
bruckerrlb is on a distinguished road
Default

Duncan, thanks a million for the explanation! That has really helped out a ton as I just could not figure it out, but with the explanation it made perfect sense.


While everything is getting compiled
Code:
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
I create a dictionary of the values from the sql results and then it gets saved to an array in the application delegate
Code:
NSDictionary *peopleDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
											 lName,  @"kLastNameKey",
											 contID, @"kContactIDKey",
											 nil];	
				
				[appDeleg.peopleArray addObject:peopleDictionary];
Then, in my table view controller, at cellForRowAtIndexPath, I did exactly what you said (minus a little modifcation)
Code:
cell.textLabel.text = [[appDeleg.peopleArray objectAtIndex:indexPath.row] objectForKey:@"kLastNameKey"];
Then, at didSelectRowAtIndex, I can get back the primary key to display the next table
Code:
NSUInteger row = [indexPath row];
		delegate.viewFirstNameHolder = [[delegate.peopleArray objectAtIndex:indexPath.row] objectForKey:@"kContactIDKey"];
		NSLog(@"Name is %@", delegate.viewFirstNameHolder);
And everything is working great! I am now off to conquer searching this table, but now that I have my dictionary in an array, should make life a lot easier than before. I really appreciate the help!!!!!!!!!!!!!
bruckerrlb is offline   Reply With Quote
Old 09-14-2010, 03:33 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 6
bruckerrlb is on a distinguished road
Default

I have a follow up question to the thread. Right now I have my array of a dictionary that has everything I need in it, contactID and lastName. This works perfectly for displaying data in a table, but my search method is throwing me off. As it is probably obvious I'm new to objective-c, so I'm not even sure where to begin on this and I've been on it for more days than I care to admit

Just to follow up from the previous post
1. I return a sql query and store it in a dictionary which then gets stored in an array
Code:
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
			{
//get sql data and store it in seperate strings called one called lName and another called contID, took this part out to shorten the code
NSDictionary *peopleDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
											 lName,  @"kLastNameKey",
											 contID, @"kContactIDKey",
											 nil];	
				
				[appDeleg.peopleArray addObject:peopleDictionary];
}
Now, the issue lies in the following:
2. In my view controller, I have a search method that searches for what the user is typing as they type, which is then added to an array and called at didSelectRowAtIndexPath to show the results. I need to find a way to make a clone of my current array w/dictionary inside, but with only the searched results
Code:
- (void) searchTableView {
	//put the searched text in a variable
	NSString *searchText = searchBar.text;
	
	//call the app delegate to bring back my array with a dictionary in it
	MyAppDelegate *appDeleg = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
	for (NSString *sTemp in [appDeleg.peopleArray valueForKey:@"kLastNameKey"])
	{
		//get a range of all of the last names
		NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
		//if it's greater than 0, add it to an array
		if (titleResultsRange.length > 0) {
			//add it to this array
			[copyListOfItems addObject:sTemp];
			//up to this point, it works fine when searching
			//but I want to get rid of copyListOfItems and put that
			//in a dictionary and then array, just like in the model
			//but only with results from the searched string so I can
			//bring them back up in the didSelectRowAtIndexPath (replace copyListOfItems which is currently there)
			
		}
		
		
}
}
So, as stated, I have no idea how to do this in objective-c and am not sure if it's even possible. Can anyone recommend a way to do this, a better method, or where I can go to find the answer?

Thanks!
bruckerrlb is offline   Reply With Quote
Reply

Bookmarks

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: 332
14 members and 318 guests
Absentia, cgokey, fiftysixty, givensur, heshiming, iGamesDev, linkmx, michaelhansen, mraalex, PixelInteractive, raihan.zbr, Sloshmonster, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,117
Posts: 402,890
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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