RLMObject NSDecimalNumber Property

I understand that RLMObjects cannot store NSDecimalNumber. To get around this, I tried the following, but failed:

    private dynamic var _amount:    String = ""
    public var amount: NSDecimalNumber {
    get { return NSDecimalNumber(string: _amount) }
    set { _amount = newValue.stringValue }
}

I get denied stating that RLMObjects cannot store NSDecimalNumbers. I got the impression that non-dynamic properties would not be stored in Realm

+4
source share
1 answer

Any property RLMObjectsshould be dynamic. Thus, the property amount: NSDecimalNumbershould be defined asdynamic

As below:

private dynamic var _amount: String = ""

public dynamic var amount: NSDecimalNumber {
    get { return NSDecimalNumber(string: _amount) }
    set { _amount = newValue.stringValue }
}

And the computed property should not be saved. (Of course, the property amount NSDecimalNumber, so it cannot be saved in Realm. If the property amountis saved, an exception has occurred)

, ignoredProperties() "amount" .

override public class func ignoredProperties() -> [AnyObject]! {
    return ["amount"]
}

, :

public class Product: RLMObject {
    private dynamic var _amount: String = ""

    public dynamic var amount: NSDecimalNumber {
        get { return NSDecimalNumber(string: _amount) }
        set { _amount = newValue.stringValue }
    }

    public override class func ignoredProperties() -> [String]! {
        return ["amount"]
    }
}
+4

All Articles