RKValueTransformers“将值转换为 NSDate 失败" [英] RKValueTransformers "failed transformation of value to NSDate"

查看:57
本文介绍了RKValueTransformers“将值转换为 NSDate 失败"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RestKit,并接收 RSS 提要.

I'm using RestKit, and pulling in a RSS feed.

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

I'm getting this error for each date related to RKValueTransformers saying (for example):

"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 的日期没有转换为字符串我可以用.

I believe that I'm pulling in the text correctly, but can't figure out if its that or if there's something else that is the reason that the date from the XML isn't transforming into a string I can use.

有什么想法吗?谢谢!

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

B远:这是我试过的:

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>

推荐答案

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

Restkit doesn't support this kind of date format out of the box, but you're in luck because it is easy to add new date formats to Restkit. First you need to create a date formatter that works for your incoming dates. I've created a date formatter for the date in your question; If you want, you can test it by plopping this code in your app delegate and checking the logs when you run:

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 对象,并且可以使用任何其他有效日期格式进行打印.不要将此代码保留在您的应用委托中...这只是为了向自己证明日期格式化程序有效.

You should see that the date is correctly converted to an NSDate object and can be printed using any other valid date format. Don't keep this code in your app delegate...this was just to prove to yourself that the date formatter works.

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

Now that you have the correct date formatter, you just need to add it to RestKit's list of date formatters so that it can properly interpret the date string that comes from your RSS feeds. You should not need to change your data model to an NSString as was suggested in the comments above.

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

This is pretty simple...during RestKit setup just drop this in:

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

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

It has been some time since I added a custom date formatter to RestKit, so if that doesn't work, check the docs to see if the syntax for adding a formatter has changed.

附言在实施和运行时要小心检查时区......我过去曾遇到过这个问题,但我相信上述方法适用于您的用例.

p.s. Be careful to check the time zones when this is implemented and running...I've had issues with that in the past, but I believe the above will work for your use case.

这篇关于RKValueTransformers“将值转换为 NSDate 失败"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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