Reskit 对象映射返回具有 nil 属性的模型对象 [英] Reskit object mapping returns model objects with nil attributes

查看:43
本文介绍了Reskit 对象映射返回具有 nil 属性的模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试映射 RSS 提要的项目.RKObjectRequestOperation 返回对象的确切数量,但属性值为零.以下是我的代码

I'm trying to map the items of the RSS feeds. The RKObjectRequestOperation returns the exact number of objects but the attributes values are nil. Following is my code

 NSURL *requestURL = [NSURL URLWithString:@"http://sports.espn.go.com/espn/rss/nfl/news"];
 [RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/rss+xml"];
RKObjectMapping *rssFeedObjectMapping = [RKObjectMapping mappingForClass:[SBRssFeed class]];
    [rssFeedObjectMapping addAttributeMappingsFromDictionary:@{
     @"title" : @"title",
     @"link" : @"link"
}];

RKResponseDescriptor *rssFeedResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:rssFeedObjectMapping
                                                                                              pathPattern:nil
                                                                                                  keyPath:@"rss.channel.item"
                                                                                              statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

 RKObjectRequestOperation *requestOperation = [[RKObjectRequestOperation alloc]initWithRequest:[NSURLRequest requestWithURL:requestURL]
                                                                              responseDescriptors:@[rssFeedResponseDescriptor]];
    [requestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        _datasourceArray = mappingResult.array;
        NSLog(@"Count %d", _datasourceArray.count);
        [self.tableView reloadData];
    }
                                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                NSLog(@"Error %@", error);
                                            }];
    [requestOperation start];

有什么我错过的吗?

推荐答案

我找到了问题所在.问题与与 RKXMLReaderSerialization 关联的 XMLReader 类有关.RKObjectMapping 类无法将字典的键与对象的属性进行映射.原因是 XMLReader 上的输出字典.

I figured out the problem. The problem was regarding to the XMLReader class associated with RKXMLReaderSerialization. The RKObjectMapping class failed to map the dictionary's keys with object's properties. The reason was the output dictionary on XMLReader.

{
    rss =     {
        channel =         {
            "atom:link" =             {
                href = "http://www.nytimes.com/services/xml/rss/nyt/Baseball.xml";
                rel = self;
                type = "application/rss+xml";
            };
            copyright =             {
                text = "Copyright 2013 The New York Times Company";
            };
            description =             {
                text = Baseball;
            };
            image =             {
                link =                 {
                    text = "http://www.nytimes.com/pages/sports/baseball/index.html?partner=rss&emc=rss";
                };
                title =                 {
                    text = "NYT > Baseball";
                };
                url =                 {
                    text = "http://graphics8.nytimes.com/images/misc/NYT_logo_rss_250x40.png";
                };
            };
            item =             (
                                {
                                    "atom:link" =                     {
                                        href = "http://www.nytimes.com/2013/01/23/sports/baseball/rays-add-right-hander-with-a-new-name.html?partner=rss&emc=rss";
                                        rel = standout;
                                    };
                                    category =                     (
                                                                    {
                                                                        domain = "http://www.nytimes.com/namespaces/keywords/nyt_org_all";
                                                                        text = "Tampa Bay Rays";
                                                                    },
                                                                    {
                                                                        domain = "http://www.nytimes.com/namespaces/keywords/des";
                                                                        text = Baseball;
                                                                    }
                                                                    );
                                    "dc:creator" =                     {
                                        text = "By THE ASSOCIATED PRESS";
                                    };
                                    description =                     {
                                        text = "The Rays agreed on a minor league contract with the right-hander Juan Carlos Oviedo, who played under the fake name Leo Nunez before an eight-week suspension by Major League Baseball last year.<img width='1' height='1' src='http://rss.nytimes.com/c/34625/f/640313/s/27cc926a/mf.gif' border='0'/><br/><br/><a href=\"http://da.feedsportal.com/r/151884969355/u/82/f/640313/c/34625/s/27cc926a/a2.htm\"><img src=\"http://da.feedsportal.com/r/151884969355/u/82/f/640313/c/34625/s/27cc926a/a2.img\" border=\"0\"/></a><img width=\"1\" height=\"1\" src=\"http://pi.feedsportal.com/r/151884969355/u/82/f/640313/c/34625/s/27cc926a/a2t.img\" border=\"0\"/>";
                                    };
                                    guid =                     {
                                        isPermaLink = false;
                                        text = "http://www.nytimes.com/2013/01/23/sports/baseball/rays-add-right-hander-with-a-new-name.html";
                                    };
                                    link =                     {
                                        text = "http://www.nytimes.com/2013/01/23/sports/baseball/rays-add-right-hander-with-a-new-name.html?partner=rss&emc=rss";
                                    };
                                    pubDate =                     {
                                        text = "Wed, 23 Jan 2013 04:01:23 GMT";
                                    };
                                    title =                     {
                                        text = "Rays Add Right-Hander With a New Name";
                                    };
                                });
            language =             {
                text = "en-us";
            };
            lastBuildDate =             {
                text = "Wed, 23 Jan 2013 04:20:27 GMT";
            };
            link =             {
                text = "http://www.nytimes.com/pages/sports/baseball/index.html?partner=rss&emc=rss";
            };
            pubDate =             {
                text = "Wed, 23 Jan 2013 04:20:27 GMT";
            };
            title =             {
                text = "NYT > Baseball";
            };
            ttl =             {
                text = 2;
            };
        };
        version = "2.0";
        "xmlns:atom" = "http://www.w3.org/2005/Atom";
        "xmlns:dc" = "http://purl.org/dc/elements/1.1/";
        "xmlns:itunes" = "http://www.itunes.com/dtds/podcast-1.0.dtd";
        "xmlns:media" = "http://search.yahoo.com/mrss/";
        "xmlns:rdf" = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
        "xmlns:taxo" = "http://purl.org/rss/1.0/modules/taxonomy/";
    };
}

与上面的响应一样,每个标签都与一个字典相关联,而字典又以文本"作为键.

As in the above response, each tag is associated with a dictionary, which in turn has 'text' as a key.

所以我把对象映射改为

    RKObjectMapping *rssFeedObjectMapping = [RKObjectMapping mappingForClass:[SBRssFeed class]];
    [rssFeedObjectMapping addAttributeMappingsFromDictionary:@{
     @"title.text" : @"title",
     @"link.text" : @"link"
}];

这解决了我的问题.

这篇关于Reskit 对象映射返回具有 nil 属性的模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆