Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to have a property as an Enum? #3152

Closed
mikailcf opened this issue Jan 29, 2016 · 4 comments
Closed

Is it possible to have a property as an Enum? #3152

mikailcf opened this issue Jan 29, 2016 · 4 comments
Assignees
Labels

Comments

@mikailcf
Copy link

enum BillState {
    case Overdue
    case Closed
    case Open
    case Future

    static func stateFromString(stateString: String) -> BillState? {
        switch stateString {
        case "overdue":
            return .Overdue
        case "open":
            return .Open
        case "closed":
            return .Closed
        case "future":
            return .Future
        default:
            return nil
        }
    }
}

class Bill: Object {
    dynamic var state: BillState
    dynamic var dueDate: NSDate
    dynamic var totalCumulative: Int

    required init() {
        super.init()
    }

    convenience init(billJSON: JSON) {
        self.init()
        ...
    }
}

Currently I cannot do that since dynamic var state: BillState gives me the error Property cannot be marked dynamic because its type cannot be represented in Objective-C.

Is there any other way for this to work with Realm?

Thanks!

@mikailcf
Copy link
Author

Well, for now I went with this solution:

enum BillState: Int {
    case Overdue  = 0
    case Closed   = 1
    case Open     = 2
    case Future   = 3

    static func stateFromString(stateString: String) -> BillState? {
        switch stateString {
        case "overdue":
            return .Overdue
        case "open":
            return .Open
        case "closed":
            return .Closed
        case "future":
            return .Future
        default:
            return nil
        }
    }
}

class Bill: Object {
    private dynamic var mappedState: Int = -1
    private dynamic var mappedDueDate: NSDate? = nil
    private dynamic var mappedTotalCumulative: Int = 0

    var state: BillState? {
        return BillState(rawValue: self.mappedState)
    }

    var dueDate: NSDate? {
        return self.mappedDueDate
    }

    var totalCumulative: Int {
        return self.mappedTotalCumulative
    }

    convenience init(billJSON: JSON) {
        self.init()
        // getting the value from a JSON with SwiftyJSON as an example
        guard let state = BillState.stateFromString(billJSON["state"].stringValue) else {
            return
        }

        self.mappedState = state.rawValue
        ...
    }
}

With that I have a nice public interface, while also, privately, having what Realm needs to do its magic.

@jpsim jpsim added the T-Help label Jan 29, 2016
@jpsim
Copy link
Contributor

jpsim commented Jan 29, 2016

Hi @mikailcf, this is how we recommend you do this. The general pattern looks like this:

class Bill: Object {
  private dynamic var rawState = -1
  public var state: BillState {
    get {
      return BillState(rawValue: rawState)!
    }
    set {
      rawState = newValue.rawValue
    }
  }

  override static func ignoredProperties() -> [String] {
    return ["state"]
  }
}

We're tracking adding better support for this in #921.

@jpsim jpsim added the pending label Jan 29, 2016
@jpsim jpsim self-assigned this Jan 29, 2016
@mikailcf
Copy link
Author

mikailcf commented Feb 1, 2016

I see, of course! Thank you!
And it's great to know you're adding support for it too!

@realm-ci realm-ci removed the pending label Feb 1, 2016
@jpsim
Copy link
Contributor

jpsim commented Feb 1, 2016

Alright, so please subscribe to #921 to get updates on our progress on that.

@jpsim jpsim closed this as completed Feb 1, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants