-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
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. |
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. |
I see, of course! Thank you! |
Alright, so please subscribe to #921 to get updates on our progress on that. |
Currently I cannot do that since
dynamic var state: BillState
gives me the errorProperty 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!
The text was updated successfully, but these errors were encountered: