0

I am trying to map JSON file to my class using Restkit. I got the problem, How can I map the nested attribute to the class.

the startTime & endTime is not mapped correctly, is there a convience way to access it ("start.datetime" does not work). I dont want to create the useless Start class, as I have to access the property like event.start.datetime. I only want to access it like event.startTime

Is there any solution?

The JSON response

{
  "kind": "calendar#event",
  "id": "f8v1p959doracohm5jk49gmbp4",
  "created": "2015-10-16T16:52:22.000Z",
  "updated": "2015-10-16T16:52:23.114Z",
  "summary": "fuck",
  "organizer": {
    "email": "jaa9s9d3gobhbmovrotujd06h8@group.calendar.google.com",
    "displayName": "primary",
    "self": true
  },
  "start": {
    "dateTime": "2015-10-11T03:00:00+08:00"
  },
  "end": {
    "dateTime": "2015-10-11T04:00:00+08:00"
  },
  "sequence": 0,
  "reminders": {
    "useDefault": true
  }
}

The class definition

class SGEvent: NSObject {
    var identifier: String!
    var title: String!
    var createdAt: NSDate?
    var updatedAt: NSDate?
    var notes: String!
    var location: String?
    var startTime: NSDate!
    var endTime: NSDate!
    var allDay: Bool! = false
}

My Code:

    let eventMapping = RKObjectMapping(forClass: SGEvent.self)
    eventMapping.addAttributeMappingsFromDictionary([
        "id":               "identifier",
        "created":          "createdAt",
        "summary":          "title",
        "location":         "location",
        "start.datetime":   "startTime",
        "end.datetime":     "endTime"
        ])
Joe SHI
  • 1,734
  • 4
  • 20
  • 39

1 Answers1

0

I have figured out the problem. It is the right syntax to access the property. The problem is with the Google's date format. 2015-10-11T03:00:00+08:00 is not a standard date format. So RestKit's default transformer cannot transfer this.

Joe SHI
  • 1,734
  • 4
  • 20
  • 39