我正在尝试使用 Restkit 将 JSON 文件映射到我的班级。我遇到了问题,如何将嵌套属性映射到类。
startTime 和 endTime 未正确映射,是否有方便的访问方式(“start.datetime”不起作用)。我不想创建无用的Start
类,因为我必须访问像 event.start.datetime 这样的属性。我只想像 event.startTime 一样访问它
有什么解决办法吗?
JSON 响应
{
"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
}
}
类定义
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
}
我的代码:
let eventMapping = RKObjectMapping(forClass: SGEvent.self)
eventMapping.addAttributeMappingsFromDictionary([
"id": "identifier",
"created": "createdAt",
"summary": "title",
"location": "location",
"start.datetime": "startTime",
"end.datetime": "endTime"
])