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.
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
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.
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.
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:
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.
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".
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.
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!!!!!!!!!!!!!
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?