RKValueTransformers “failed transformation of valu

2019-01-12 12:26发布

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

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."

It goes on to tell me:

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

and:

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

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.

Any ideas? Thanks!

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

BFar: This is what I've tried:

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

with your help of:

    // 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条回答
一夜七次
2楼-- · 2019-01-12 13:05

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]);

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.

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.

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];

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.

查看更多
登录 后发表回答