Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
agreatdaytocode
24,757 PointsNSDictionary with keys issue
What would be the best way to pull information from a dictionary if I only wanted some of the information?
Below is an example of the information being pulled back. I only need the description.
2014-06-08 15:37:15.399 TC[1381:4207] (
{
dateAdded = "2014-04-21T13:22:15Z";
id = 7473;
type = "[Acme] Assigned To";
value = "Analyst";
},
{
dateAdded = "2014-04-21T13:21:44Z";
id = 7472;
type = "[Acme] Date Briefed ";
value = "04-26-2014 00:00 UTC";
},
{
dateAdded = "2014-04-21T13:21:29Z";
id = 7471;
type = "Description";
value = "This is the description"; //I want this information!
},
3 Answers
Brett Doffing
4,978 PointsFor sure! Try:
for (NSDictionary *myDict in myArray) {
NSString *myString = [myDict valueForKey:@"type"];
if ([myString isEqualToString:@"Description"]) {
NSLog(@"%@", [myDict valueForKey:@"value"]);
}
}
Brett Doffing
4,978 Points[myDictonary valueForKey:@"dateAdded"];
Or objectForKey
agreatdaytocode
24,757 PointsI'll give it a shot! thanks.
agreatdaytocode
24,757 PointsYeah, It's not working
2014-06-08 20:32:15.553 App[3328:162915] LabelValue is =====> (null)
Problem is the keys are all the same, but the values are different. It has 4+ keys that are all named value. I just need the one that has the description. Not sure if its possible.
Brett Doffing
4,978 PointsIt almost looks like you have an array of dictionaries? If that is the case then it would be more like:
NSDictionary *myDict = [myArray objectAtIndex:2];
Then
[myDict valueForKey:@"value"];
To get the value specified in your original post.
agreatdaytocode
24,757 Pointsright, problem is its NEVER in the same order, and some of them don't have a description. :(
Brett Doffing
4,978 PointsThen there is a problem when you are creating the array, and when you say description, do you mean the value for a key?
agreatdaytocode
24,757 PointsYeah, I need the pull the Key "value" that holds the description.
Is is possible to filter on the other Keys to figure out what value to pull?
{
dateAdded = "2014-04-21T13:21:29Z";
id = 7471;
type = "Description"; //This tells me that the value will hold the description I am looking for.
value = "This is the description"; //I want this information!
},
dateAdded = "2014-04-21T13:21:44Z";
id = 7472;
type = "[Acme] Date Briefed ";
value = "04-26-2014 00:00 UTC"; // I don't need or want this value key
},
Thanks for your time btw. Also if you want to see it just shoot me an email I'll share the project with you over a private github. Aaron at Mav3r1ck.com
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsThat worked! thanks.