Using enum as property of Realm model

后端 未结 5 500
感动是毒
感动是毒 2020-12-07 16:34

Is it possible to use an Enum as a property for my model? I currently have a class like this:

class Checkin: RLMObject {
  dynamic var id: Int = 0
  dynamic          


        
5条回答
  •  眼角桃花
    2020-12-07 17:01

    Since Realm support Objective-C enums and they are representable by Int you can use this:

    class Checkin: Object {
      dynamic var id: Int = 0
      dynamic var kind: Kind = .checkedIn
    
      @objc enum Kind: Int {
        case checkedIn
        case enRoute
        case droppedOff
      }
      ....
    }
    

    If you need to parse to/from String you can use a custom initializer for Kind and an toString function.

    There is a discussion about this in GitHub

    This works with Swift 3.0 and Realm 2.0.2

提交回复
热议问题