草庐IT

ios - Reskit 对象映射返回具有 nil 属性的模型对象

coder 2024-01-26 原文

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

 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 上的输出字典。

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

在上面的响应中,每个标签都与一个字典相关联,而字典又以“文本”作为键。

所以我把对象映射改成了

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

这解决了我的问题。

关于ios - Reskit 对象映射返回具有 nil 属性的模型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233934/

有关ios - Reskit 对象映射返回具有 nil 属性的模型对象的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  4. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  7. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  8. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  9. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  10. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

随机推荐