سؤال

So I have JSON data from http://blog.teamtreehouse.com/api/get_recent_summary/ which I placed into a dictionary object named dataDictionary after using NSJSONSerialization to parse the data:

 NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error: &error];

I then put the posts key into my blogPosts array like this:

self.blogPosts = [dataDictionary objectForKey:@"posts"];

Then I used that array to retrive some info by putting it back to a dictionary using the following:

 NSDictionary *blogPost = [self.blogPosts objectAtIndex:someNumber];
 cell.textLabel.text = [blogPost valueForKey:@"title"];

Can someone explain what is going on here between the array and dictionary? My confusion arises from thinking that an array can only hold information in each of its indexes, like a string or even another array and not key/value pairs. Even if the posts key is an array of arrays, how does the blogPosts array understand key/value pairs in order for the blogPost dictionary to retrieve data by calling valueForKey.

Simple english would be great, I'm in the beginning phase of learning Obj-C.

هل كانت مفيدة؟

المحلول

You have a dictionary from your JSON

Where key: "posts" = an array of individual dictionaries

So we received a Dictionary, and we removed the object stored at key "posts" which is an array. This array happens to contain more dictionaries. So when we get our blog post,

NSDictionary *blogPost = self.blogPosts[someNumber];

We receive a new dictionary that was stored in the array.

Main dictionary >> array of posts >> post as dictionary

I wanted to say as well, that the array doesn't contain key/value pairs. Just as you said an array can be an array of arrays, an array can be an array of dictionaries.

نصائح أخرى

An example what JSON can look like:

{
    "smallnumbers": [1,2,3,4],
    "bignumbers": [100,200,1000,10000,100000]
}

See, you have a dictionary with two keys, and each key has an array as its value.

A dictionary can have any number of key/value pairs. For JSON, the keys must be strings, and the values can be arrays, dictionaries, strings, numbers, booleans, or the null value.

An array can have any number of elements. The elements can be arrays, dictionaries, strings, numbers, booleans, or the null value.

In JSON's specs, everything between curly braces ({ }) defines an object, meaning that each value is associated to a string key. Everything between square brackets ([ ]) defines an array, which means it's a simple list with a sequential, numerical index.

Example:

{
    "myFirstKey"    : "My first value",
    "mySecondKey"   : "My second value",
    "myFirstArray"  : [
        "First Array Element",
        "Second Array Element",
    ],
    "mySecondArray" : [
        {
            "myFirstSubKey"  : "My first sub value (object 1)",
            "mySecondSubKey" : "My second sub value (object 1)"
        },
        {
            "myFirstSubKey"  : "My first sub value (object 2)",
            "mySecondSubKey" : "My second sub value (object 2)"
        }
    ]
}

The root object would be an NSDictionary (starts with {).

You can access all entries of the dictionary by calling the objectForKey: selector. The return type is id so the type can change. Literal values, meaning that the value doesn't start with a curly brace or a square bracket, will have the appropriate type (NSString or NSNumber). Everything else will be either an NSDictionary (if it starts with a curly brace) or an NSArray (if it starts with a square bracket).

Hope this helps.

Your misunderstanding is that [dataDictionary objectForKey:@"posts"] is not an array of arrays — it's an array of dictionaries. So you have a dictionary — dataDictionary — which contains an array —dataDictionary[@"posts"] — which contains one or more dictionaries. You are assigning one of these dictionaries to the variable declared as NSDictionary *blogPost.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top