wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
264 stars 109 forks source link

Field names causing NSUnknownKeyException #119

Closed Croge32 closed 7 years ago

Croge32 commented 7 years ago

In our project, we have fields named "description", "start-time", and "end-time".

Because I cannot use hyphens in variable names in Swift, and because Resource contains a description field already, I constructed my model as follows:

class EventModel: Resource {

  var title: String?
  var date: Date?
  var startTime: String?
  var endTime: String?
  var descriptionText: String?

  var resource: ResourceModel?
  var reservation: ReservationModel?

  override class var resourceType: ResourceType {
    return "events"
  }

  override class var fields: [Field] {
    return fieldsFromDictionary([
      "title": Attribute(),
      "date": DateAttribute(),
      "start-time": Attribute(),
      "end-time": Attribute(),
      "description": Attribute(),

      "resource": ToOneRelationship(ResourceModel.self),
      "reservation": ToManyRelationship(ReservationModel.self)
      ])
  }

...

This setup causes me to have this error when dealing with start-time, end-time, and/or description:

2016-10-11 11:27:52.912 Office App[9723:451763] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Office_App.EventModel 0x6080003efe00> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key start-time.'

Is there some configuration I'm using incorrectly or some way I can bypass this issue without having to rename fields in our API?

OliverDobner-flinc commented 7 years ago

@Croge32 You have to use the override like this: ... "startTime": Attribute().serializeAs("start-time") ... and so on.