Thursday, 9 April 2015

Swift Restkit Mapping Data and Date format


In Swift, your object should define like this:


class Order : NSObject {
    var orderId: String = ""

    var createdAt: NSDate?

And if your backend returns your data in this format "2015-03-17 15:14:12.971Z"  (of cause it is a string)

Then, you don't need do any extra jobs, Restkit will convert this time into NSDate for you.



if your backend gives your different format of date you can:

https://github.com/RestKit/RestKit/wiki/Configuring-Date-and-Time-Mapping



If you want to display the date in a particular format, you can:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd 'at' h:mm a" // superset of OP's format
let str = dateFormatter.stringFromDate(NSDate())

No comments:

Post a comment