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

Enum Attributes? #118

Closed Croge32 closed 7 years ago

Croge32 commented 7 years ago

So I have created a class that appears as follows:

class ResourceModel: Resource {

  var category: Category?
  var title: String?

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

  override class var fields: [Field] {
    return fieldsFromDictionary([
      "category": Attribute(),
      "title": Attribute()
      ])
  }

  init(category: Category?, title: String?) {
    self.category = category
    self.title = title
    super.init()
  }

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

  required init() {
    super.init()
  }

  enum Category {
    case parking_office, parking_cb, conference_room
  }

}

When running a findAll on my class, I get this error:

2016-10-10 12:04:19.607 Office App[6057:227184] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Office_App.ResourceModel 0x608000ee8480> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key category.'

When switching my category field to a String for testing, this error goes away.

Is there something I'm doing improperly? How would I go about supporting an enum?

wvteijlingen commented 7 years ago

Unfortunately key value coding only works with NSObjects. This is one of the biggest downsides of using Spine. So you have to make your Category type a class.

Croge32 commented 7 years ago

Gotcha, thanks!