【问题标题】:RKValueTransformers "failed transformation of value to NSDate"RKValueTransformers“将值转换为 NSDate 失败”
【发布时间】:2014-09-04 22:19:21
【问题描述】:

我正在使用 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 中的日期没有转换为字符串 我可以使用。

有什么想法吗?谢谢!

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

BF远: 这是我尝试过的:

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>

【问题讨论】:

  • ISO 8601 是一个类似于2014-09-03T15:23:34+00:00 的日期。关键是日期和时间之间的“T”。见en.wikipedia.org/wiki/…
  • 为什么日期没有映射?就像它甚至不会显示在 dateLabel 中一样,它只会显示 null。我想这个问题更多的是关于那个,如果我说得不够清楚,对不起。
  • 因为它与预期的格式不匹配。
  • 我的意思是,如果我什至不能把它拉到任何地方,我怎么能得到那个字符串呢?
  • 如果是null,我什至无法获取值并编写转换日期的方法你知道吗?

标签: xml nsdate restkit


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

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

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

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

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

附言请注意检查实施和运行时的时区...我过去曾遇到过问题,但我相信以上内容适用于您的用例。

【讨论】:

  • 天哪,伙计。是的,这是完美的。我已经可以说出来了。我会留意时区的事情。说真的,非常感谢您的精彩解释!
  • 实际上我仍然遇到同样的问题。在上面的BFar下发布了上面的代码。但主要是我的 dateString 从 XML 进来是 null,所以我什至无法开始日期格式化过程你知道吗?
  • 我将您硬编码的dateStr 换成了我实际上将从XML 中恢复的那个,我认为这是有道理的。我从 XML 中得到的仍然是 null。你能帮我吗?谢谢!
  • 嗯,我不确定我是否正确地遵循了您的所有代码,但让我们尝试弄清楚...为什么要将 pubDate.text 映射到 pubDate? Restkit 应该能够从您的 XML 中获取日期字符串并将其作为 NSDate 对象返回(这就是 RKValueTransformer 将在我们确保正确配置后执行的操作)。在我看来,pubDate 应该映射到 pubDate。
  • 我正在关注这个人在他的问题/答案中所做的事情:stackoverflow.com/questions/14233934/…
猜你喜欢
  • 1970-01-01
  • 2011-01-09
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
相关资源
最近更新 更多