目标C从完成模块中获取数据并在外部类中进行解析

时间:2018-07-02 02:16:49

标签: ios objective-c

我有一个应用程序,其代码与许多联网应用程序相似。我想将JSON有效负载从完成块中取出,放入数组中,并通过其他类枚举来创建对象。然后,我希望这些对象填充tableView。

我的问题是我拥有的解决方案在完成模块中需要NSMutableArray类型的许多属性。我只看过带有注释的演示//对数据有帮助。只是很难处理解析,调用其他类上的方法以及从完成块内部填充数组。我可以,但是代码变成了意大利面条。

对此有什么好的架构?

if (request) {
        [client sendTwitterRequest:request completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (data) {
                // handle the response data e.g.
                NSLog(@"DID data request start");
                NSError *jsonError;
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
[self handleJSON:json];
                }
            }
            else {
                NSLog(@"Error: %@", connectionError);
            }
        }];
    }
    else {
        NSLog(@"Error: %@", error);
    }

我像这样创建一个辅助方法

-(void)handleJSON:(NSDictionary*)dictionary{

    for (NSDictionary *jsonDict in dictionary) {

        id value = [jsonDict valueForKey:@"text"];

        NSLog(@"string text %@", value);

    }

但是我得到

  

此类不符合键文本的键值编码。'

JSON如下:

{
    "search_metadata" =     {
        "completed_in" = "0.041";
        count = 15;
        "max_id" = 1013632106432360448;
        "max_id_str" = 1013632106432360448;
        "next_results" = "?max_id=1013632026514079744&q=%23trump&include_entities=1";
        query = "%23trump";
        "refresh_url" = "?since_id=1013632106432360448&q=%23trump&include_entities=1";
        "since_id" = 0;
        "since_id_str" = 0;
    };



    statuses =     (
                {
            contributors = "<null>";   
            coordinates = "<null>";   
            "created_at" = "Mon Jul 02 03:55:01 +0000 2018";   
            entities =             {
                hashtags =                 (
                                        {
                indices =                         (
                            20,
                            28
                        );
                        text = GOODWIN;

当我快速枚举以获取数组中的字典时,数组计数为2-一个用于search_metadata,另一个用于statuses。在statuses中有几条鸣叫。我以前从未遇到过这样的事情。

0 个答案:

没有答案