RKValueTransformers“未能将价值转换为NSDate”

时间:2014-09-04 22:19:22

标签: xml nsdate restkit

我正在使用RestKit,并提取RSS源。

对于与RKValueTransformers 相关的每个日期,我收到此错误(例如):

"Failed transformation of value 'September 04, 2014 09:58:01 PDT' to NSDate: none of the 10 value transformers consulted were successful."

接着告诉我:

Failed transformation of value at keyPath 'pubDate.text' to representation of type 'NSDate'

{NSLocalizedDescription=Input value is not a valid ISO 8601 string: 'September 01, 2014 14:53:30 PDT'}

我相信我正确地提取了文本,但是无法弄清楚它是否存在或者是否存在其他原因 XML中的日期未转换为字符串< / strong>我可以使用。

有什么想法吗?谢谢!

更新: @property (nonatomic, strong) NSDate *pubDate;

BFAR: 这就是我尝试过的:

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

在你的帮助下:

    // My Date String from XML
    NSString *dateString = [NSString stringWithFormat:@"%@", feedDan.pubDate];

    // String in XML Feed has format of: "September 04, 2014 09:58:01 PDT"

    // New
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"MMMM dd, yyyy HH:mm:ss z"];
    //[RKObjectMapping addDefaultDateFormatter:dateFormatter];
    // Above line is deprecated: "Configure [RKValueTransformer defaultValueTransformer] instead"
    // Not sure if below is correct
    [[RKValueTransformer defaultValueTransformer] addValueTransformer:dateFormatter];
    NSDate *date = [dateFormatter dateFromString:dateString];

    NSDateFormatter *otherFormatter = [NSDateFormatter new];
    // API shows "2014-09-04T20:22:05Z", but prints in label as 2014-08-27 16:14:04 +0000
    [otherFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];  // "2014-09-04T20:22:05Z" to 2014-08-27 16:14:04 +0000
    NSLog(@"Stack: %@",[otherFormatter stringFromDate:date]);

XML:

<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<meta http-equiv="Content-type" content="text/xml; charset=utf-8"/>
<channel>
<title>ESPN Feed: green bay packers</title>
<link><![CDATA[http://search.espn.go.com/green-bay-packers/]]></link>
<image>
<url>http://assets.espn.go.com/i/tvlistings/tv_espn_original.gif</url>
<title>ESPN</title>
<link><![CDATA[http://search.espn.go.com/green-bay-packers/]]></link>
</image>
<language>en-us</language>
<ttl>10</ttl>
<generator>ESPN Inc. http://espn.go.com/</generator>
<copyright>Copyright 2010 ESPN Inc.</copyright>
<item>
<title><![CDATA[Sherman wants a little more action]]></title>
<link><![CDATA[http://espn.go.com/blog/seattle-seahawks/post/_/id/8444/sherman-wants-a-little-more-action]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/blog/seattle-seahawks/post/_/id/8444/sherman-wants-a-little-more-action]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/blog/seattle-seahawks/post/_/id/8444/sherman-wants-a-little-more-action&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/blog/seattle-seahawks/post/_/id/8444/sherman-wants-a-little-more-action" height="49" width="41"/></a>
SEATTLE -- Seattle Seahawks cornerback Richard Sherman was a little down after Thursday's 36-16 season-opening victory over the Green Bay Packers. He...]]></description>
<pubDate>September 06, 2014 05:00:45 PDT</pubDate>
</item>
<item>
<title><![CDATA[Fantasy Football: Week 1 Cheat Sheet]]></title>
<link><![CDATA[http://insider.espn.go.com/fantasy/football/story/_/id/11472204/week-1-cheat-sheet]]></link>
<guid isPermaLink="true"><![CDATA[http://insider.espn.go.com/fantasy/football/story/_/id/11472204/week-1-cheat-sheet]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://insider.espn.go.com/fantasy/football/story/_/id/11472204/week-1-cheat-sheet&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://insider.espn.go.com/fantasy/football/story/_/id/11472204/week-1-cheat-sheet" height="49" width="41"/></a>
Tim Kavanagh shares the most essential fantasy tips you need to set your Week 1 lineup, all distilled into one handy file.]]></description>
<pubDate>September 05, 2014 12:11:07 PDT</pubDate>
</item>
<item>
<title><![CDATA[CB Lane out indefinitely]]></title>
<link><![CDATA[http://espn.go.com/blog/seattle-seahawks/post/_/id/8439/cb-jeremy-lane-out-indefinitely]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/blog/seattle-seahawks/post/_/id/8439/cb-jeremy-lane-out-indefinitely]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/blog/seattle-seahawks/post/_/id/8439/cb-jeremy-lane-out-indefinitely&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/blog/seattle-seahawks/post/_/id/8439/cb-jeremy-lane-out-indefinitely" height="49" width="41"/></a>
Seattle Seahawks coach Pete Carroll said cornerback Jeremy Lane could miss a significant amount of time after re-injuring his groin in Thursday night'...]]></description>
<pubDate>September 05, 2014 16:10:31 PDT</pubDate>
</item>
<item>
<title><![CDATA[Green Bay Packers coach Mike McCarthy says team was not intentionally avoiding Richard Sherman]]></title>
<link><![CDATA[http://espn.go.com/nfl/story/_/id/11473071/green-bay-packers-coach-mike-mccarthy-says-team-was-not-intentionally-avoiding-richard-sherman]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/nfl/story/_/id/11473071/green-bay-packers-coach-mike-mccarthy-says-team-was-not-intentionally-avoiding-richard-sherman]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/nfl/story/_/id/11473071/green-bay-packers-coach-mike-mccarthy-says-team-was-not-intentionally-avoiding-richard-sherman&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/nfl/story/_/id/11473071/green-bay-packers-coach-mike-mccarthy-says-team-was-not-intentionally-avoiding-richard-sherman" height="49" width="41"/></a>
Aaron Rodgers and the Green Bay Packers never challenged cornerback Richard Sherman once in Thursday's 36-16 loss to the Seattle Seahawks.]]></description>
<pubDate>September 05, 2014 14:50:42 PDT</pubDate>
</item>
<item>
<title><![CDATA[Eddie Lacy of Green Bay Packers progressing after sustaining concussion]]></title>
<link><![CDATA[http://espn.go.com/nfl/story/_/id/11472685/eddie-lacy-green-bay-packers-progressing-sustaining-concussion]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/nfl/story/_/id/11472685/eddie-lacy-green-bay-packers-progressing-sustaining-concussion]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/nfl/story/_/id/11472685/eddie-lacy-green-bay-packers-progressing-sustaining-concussion&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/nfl/story/_/id/11472685/eddie-lacy-green-bay-packers-progressing-sustaining-concussion" height="49" width="41"/></a>
The last time Eddie Lacy sustained a concussion, the Green Bay Packers running back missed the following game. He appears to be on track for a quicker return this time.]]></description>
<pubDate>September 05, 2014 13:18:59 PDT</pubDate>
</item>
<item>
<title><![CDATA[2014 NFL Gambling Guide]]></title>
<link><![CDATA[http://grantland.com/the-triangle/2014-nfl-gambling-guide/]]></link>
<guid isPermaLink="true"><![CDATA[http://grantland.com/the-triangle/2014-nfl-gambling-guide/]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://grantland.com/the-triangle/2014-nfl-gambling-guide/&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://grantland.com/the-triangle/2014-nfl-gambling-guide/" height="49" width="41"/></a>
Football, football, football, football! FOOTBALL!!!!!!!! If you missed my Thursday column about Roger Goodell's NFL That Never Sleeps (featuri...]]></description>
<pubDate>September 05, 2014 13:00:50 PDT</pubDate>
</item>
<item>
<title><![CDATA[Fantasy: Eddie Lacy]]></title>
<link><![CDATA[http://espn.go.com/video/clip?id=11472238]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/video/clip?id=11472238]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/video/clip?id=11472238&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/video/clip?id=11472238" height="49" width="41"/></a>
Christopher Harris discusses Eddie Lacy's value after Week 1.]]></description>
<pubDate>September 05, 2014 12:13:36 PDT</pubDate>
</item>
<item>
<title><![CDATA[Fantasy: Aaron Rodgers]]></title>
<link><![CDATA[http://espn.go.com/video/clip?id=11472243]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/video/clip?id=11472243]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/video/clip?id=11472243&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/video/clip?id=11472243" height="49" width="41"/></a>
Christopher Harris discusses Aaron Rodgers' value after Week 1.]]></description>
<pubDate>September 05, 2014 12:15:35 PDT</pubDate>
</item>
<item>
<title><![CDATA[Short to-do list for the Seahawks]]></title>
<link><![CDATA[http://espn.go.com/blog/seattle-seahawks/post/_/id/8409/short-to-do-list-for-the-seahawks]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/blog/seattle-seahawks/post/_/id/8409/short-to-do-list-for-the-seahawks]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/blog/seattle-seahawks/post/_/id/8409/short-to-do-list-for-the-seahawks&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/blog/seattle-seahawks/post/_/id/8409/short-to-do-list-for-the-seahawks" height="49" width="41"/></a>
SEATTLE -- An examination of what the Seattle Seahawks must do after their 36-16 victory over the Green Bay Packers Thursday night at CenturyLink Fiel...]]></description>
<pubDate>September 05, 2014 12:00:28 PDT</pubDate>
</item>
<item>
<title><![CDATA[Should The Packers Be Worried?]]></title>
<link><![CDATA[http://espn.go.com/video/clip?id=11471863]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/video/clip?id=11471863]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/video/clip?id=11471863&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/video/clip?id=11471863" height="49" width="41"/></a>
John Clayton discusses if there's reason for concern in Green Bay and if any team will challenge the Seahawks in the NFC West.]]></description>
<pubDate>September 05, 2014 11:20:15 PDT</pubDate>
</item>
<item>
<title><![CDATA[Viewers of NFL opener up seven percent from last year]]></title>
<link><![CDATA[http://espn.go.com/nfl/story/_/id/11472029/viewers-nfl-opener-seven-percent-last-year]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/nfl/story/_/id/11472029/viewers-nfl-opener-seven-percent-last-year]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/nfl/story/_/id/11472029/viewers-nfl-opener-seven-percent-last-year&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/nfl/story/_/id/11472029/viewers-nfl-opener-seven-percent-last-year" height="49" width="41"/></a>
Fans eagerly welcomed the NFL's return on Thursday night.]]></description>
<pubDate>September 05, 2014 11:40:52 PDT</pubDate>
</item>
<item>
<title><![CDATA[NFL Season Predictions, Part 4: The Contenders]]></title>
<link><![CDATA[http://grantland.com/the-triangle/nfl-season-predictions-teams-that-can-win-super-bowl/]]></link>
<guid isPermaLink="true"><![CDATA[http://grantland.com/the-triangle/nfl-season-predictions-teams-that-can-win-super-bowl/]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://grantland.com/the-triangle/nfl-season-predictions-teams-that-can-win-super-bowl/&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://grantland.com/the-triangle/nfl-season-predictions-teams-that-can-win-super-bowl/" height="49" width="41"/></a>
Finally, after thousands of words, dozens of articles, and 24 team capsules, we arrive at the eight teams that have the best chance of winning the Sup...]]></description>
<pubDate>September 05, 2014 10:29:55 PDT</pubDate>
</item>
<item>
<title><![CDATA[About Last Night: Still Federer]]></title>
<link><![CDATA[http://grantland.com/the-triangle/about-last-night-still-federer/]]></link>
<guid isPermaLink="true"><![CDATA[http://grantland.com/the-triangle/about-last-night-still-federer/]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://grantland.com/the-triangle/about-last-night-still-federer/&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://grantland.com/the-triangle/about-last-night-still-federer/" height="49" width="41"/></a>
In case you were busy making last night the last night, here are the top five stories you missed in sports on Thursday: Roger Federer overcame a two-...]]></description>
<pubDate>September 05, 2014 10:13:13 PDT</pubDate>
</item>
<item>
<title><![CDATA[Aaron Rodgers Has Big Night On Social Media, If Not On the Field]]></title>
<link><![CDATA[http://espn.go.com/blog/espnw/post/_/id/14071/aaron-rodgers-has-big-night-on-social-media-if-not-on-the-field]]></link>
<guid isPermaLink="true"><![CDATA[http://espn.go.com/blog/espnw/post/_/id/14071/aaron-rodgers-has-big-night-on-social-media-if-not-on-the-field]]></guid>
<description><![CDATA[
<a href="http://api.tweetmeme.com/share?url=http://espn.go.com/blog/espnw/post/_/id/14071/aaron-rodgers-has-big-night-on-social-media-if-not-on-the-field&amp;service=tinyurl.com&amp;source=espn"><img style="padding-left:10px;" align="right" border="0" style="border:none;" src="http://api.tweetmeme.com/imagebutton.gif?url=http://espn.go.com/blog/espnw/post/_/id/14071/aaron-rodgers-has-big-night-on-social-media-if-not-on-the-field" height="49" width="41"/></a>
Despite an embarrassing 36-16 loss to the Seattle Seahawks in Thursday's NFL opener, Green Bay Packers QB Aaron Rodgers still had a fairly impressive ...]]></description>
<pubDate>September 05, 2014 10:14:49 PDT</pubDate>
</item>
</channel>
</rss>

1 个答案:

答案 0 :(得分:1)

Restkit不支持开箱即用的这种日期格式,但是你很幸运,因为很容易向Restkit添加新的日期格式。首先,您需要创建一个适用于您的传入日期的日期格式化程序。我已经在你的问题中为日期创建了日期格式化程序;如果需要,可以通过在应用程序委托中填写此代码并在运行时检查日志来测试它:

NSString * dateStr = @"September 01, 2014 14:53:30 PDT";

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"MMMM dd, yyyy HH:mm:ss z"];
NSDate *date = [dateFormatter dateFromString:dateStr];

NSDateFormatter *otherFormatter = [NSDateFormatter new];
[otherFormatter setDateStyle:NSDateFormatterFullStyle];
NSLog(@"%@",[otherFormatter stringFromDate:date]);

您应该看到日期已正确转换为NSDate对象,并且可以使用任何其他有效日期格式进行打印。不要将此代码保留在您的app委托中......这只是为了向自己证明日期格式化程序有效。

现在您拥有了正确的日期格式化程序,您只需将其添加到RestKit的日期格式化程序列表中,以便它可以正确解释来自RSS源的日期字符串。您不需要像上面的评论中所建议的那样将数据模型更改为NSString。

这很简单......在RestKit设置期间,只需将其放入:

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"MMMM dd, yyyy HH:mm:ss z"];
[RKObjectMapping addDefaultDateFormatter:dateFormatter];

自从我向RestKit添加自定义日期格式化程序已经有一段时间了,所以如果这不起作用,请检查文档以查看添加格式化程序的语法是否已更改。

P.S。实施和运行时要小心检查时区...我过去曾遇到过这方面的问题,但我相信上述内容适用于您的用例。