Search code examples
objective-cweb-servicesnsmutabledictionaryjsonkitdrupal-services

How do i deal with my response object which corresponds to node ids?


Edit wording..

I am using a 3rd party library called Drupal-IOS-SDk to connect my Drupal website with my under development IOS app. I use a method to index nodes on my website and I am just wondering if anyone has any knowledge about how to deal with the response from my website. To give a little context if I run a similar bit of code to my index method (a getNode method) which works fine and I am able to access my response perfectly as shown below:

    //code for correctly working nodeGet method

NSMutableDictionary *nodeData = [NSMutableDictionary new];

[nodeData setValue:@"650" forKey:@"nid"];
[DIOSNode nodeGet:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {

    //print out responseObject
    //NSLog(@"%@", responseObject);

    //pull data from the responseObject
    NSInteger testNumber = [responseObject objectForKey:@"changed"];
    NSLog(@"%ld", (long)testNumber);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //we failed, uh-oh lets error log this.
    NSLog(@"%@,  %@", [error localizedDescription], [operation responseString]);
 }];

This is the what gets printed by response object(I didnt include the whole thing but youll get the point):

    //printed statement for correctly working nodeGet method

{
changed = 1390534644;

comment = 0;

created = 1390534644;

data = "b:0;";

"ds_switch" = "";

"field_additional_pictures" =     (
);
"field_author" =     {
    und =

The above code gets node data and calling the "objectforkey" method on responseObject lets me access numbers or whatever else is stored in my responseObject. Where I have commented pull data from response object I get back the integer "1390534644" which correctly corresponds to the "changed" variable as you can see from the printed response above. The above section works fine. It is the next step where I get confused:

//code for index method that is in question

NSMutableDictionary *paramsDictionary = [NSMutableDictionary new];
[paramsDictionary setValue:@"books_e_books_other_books" forKey:@"type"];
[DIOSNode nodeIndexWithPage:@"0" fields:@"nid" parameters:paramsDictionary pageSize:@"20"
                    success:^(AFHTTPRequestOperation *operation, id responseObject) {
                        //print response object
                        NSLog(@"%@", responseObject);
                        NSLog(@"GREAT SUCCESS");

                        //HERE IS MY PROBLEM!!!
                        //what do I do with response object?

                    }
                    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                        //code
                        NSLog(@"Oh no failed when trying to get the index");
                        NSLog(@"%@,  %@", [error localizedDescription], [operation responseString]);
                    }];

In this section I index all of the nodes and get fields from each instead of getting all of the information from only one node. I get a response which shows things are working correctly thus far because the response has the correct info. I am confused though because I am not sure what my response object is exactly. It is a collection of nodes each with a "nid" and "uri" as shown from the index method responseObject below. If I wanted to get the value "650" for example from my first "nid" in the below printed area how would I go about doing this? I dont think I can call "objectForKey" as I did in the first working example because each node has a "nid". If I told my app to look for a key named nid it doesnt know which one to look for. With no unique keys how can I access the number 650? I have printed my index method responseObject below so you can see what I am talking about.

//printed statement from index method, this is what i am confused about, there are no unique keys how do I access the value 650 or the first nid

 {

    nid = 650;

    uri = "http://www.domain.com/domain_endpoint/node/650";
},

    {

    nid = 649;

    uri = "http://www.domain.com/domain_endpoint/node/649";
},

    {

    nid = 647;

    uri = "http://www.domain.com/domain_endpoint/node/647";
},

Solution

  • It's been a few months since you asked this question, so I'm not sure if you're still looking for an answer, but in case anyone in the future needs it.

    The responseObject is an array of NSDictionary objects. You can access the responseObject items just like an array and do whatever you need to with them depending on what you need the data for.

    For example:

    NSArray *responseArray = responseObject;
    
    for (NSDictionary *item in responseArray)
    
    {
        // Do something with your item here
        // For example:
        NSString *uriStr = [item valueForKey:@"uri"]; 
    }