1

编辑措辞..

我正在使用名为 Drupal-IOS-SDk 的第 3 方库将我的 Drupal 网站与我正在开发的 IOS 应用程序连接起来。我使用一种方法来索引我网站上的节点,我只是想知道是否有人知道如何处理来自我网站的响应。如果我运行与我的 index 方法(getNode 方法)类似的代码,可以提供一些上下文,它工作正常,我能够完美地访问我的响应,如下所示:

    //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]);
 }];

这是响应对象打印的内容(我没有包括整个内容,但您会明白的):

    //printed statement for correctly working nodeGet method

{
changed = 1390534644;

comment = 0;

created = 1390534644;

data = "b:0;";

"ds_switch" = "";

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

上面的代码获取节点数据并调用 responseObject 上的“objectforkey”方法让我可以访问数字或存储在我的 responseObject 中的任何其他内容。在我从响应对象中评论拉取数据的地方,我得到了整数“1390534644”,它正确对应于“更改”变量,正如您从上面的打印响应中看到的那样。上面的部分工作正常。这是我感到困惑的下一步:

//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]);
                    }];

在本节中,我索引所有节点并从每个节点获取字段,而不是仅从一个节点获取所有信息。我收到一个响应,显示到目前为止一切正常,因为响应包含正确的信息。不过我很困惑,因为我不确定我的响应对象到底是什么。它是一个节点集合,每个节点都有一个“nid”和“uri”,如下面的索引方法 responseObject 所示。例如,如果我想从下面打印区域中的第一个“nid”中获取值“650”,我将如何执行此操作?我不认为我可以像在第一个工作示例中那样调用“objectForKey”,因为每个节点都有一个“nid”。如果我告诉我的应用程序查找名为 nid 的键,它不知道要查找哪个键。如果没有唯一键,我如何访问数字 650?我在下面打印了我的索引方法 responseObject,所以你可以看到我在说什么。

//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";
},
4

1 回答 1

2

你问这个问题已经几个月了,所以我不确定你是否还在寻找答案,但以防将来有人需要它。

responseObject 是一个 NSDictionary 对象的数组。您可以像访问数组一样访问 responseObject 项,并根据您需要数据的目的对它们进行任何操作。

例如:

NSArray *responseArray = responseObject;

for (NSDictionary *item in responseArray)

{
    // Do something with your item here
    // For example:
    NSString *uriStr = [item valueForKey:@"uri"]; 
}
于 2014-05-07T21:27:50.213 回答