Browsing archives for 'Restkit'

Restkit Parse ActiveRecord Timestamp

Restkit,RubyOnRails,iOS Dev 28 十月 2011 | View Comments

If you are using Rails as your backend, Reskit will have trouble to parse ActiveRecord timestamp.

Default ActiveRecord timestamp JSON out is 2008-12-29T00:27:42-08:00

You can not convert it to NSDate directly use NSDateFomatter
you must convert it to 2008-12-29T00:27:42-0800

But you have not chance to convert when you use Restkit Mapping.

Here is my solution
1. Monkey patch Time and DateTime class

class Time
  def as_json(option=nil)
    self.strftime("%Y-%m-%dT%H:%m:%S%z")
  end
end
class DateTime
  def as_json(format=nil)
    self.strftime("%Y-%m-%dT%H:%m:%S%z")
  end
end

2. Add default date fomatter to RKObjectMapping

    NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    [RKObjectMapping addDefaultDateFormatter:dateFormatter];


Tagged in , ,

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.