I can't save data as enum in Realm. When I track the state of a variable, and save, the old value remains, instead of the new one. What am I doing wrong?
I used a setter and a getter, but still the problem is not solved.
import RealmSwift
class Transaction: Object {
var accounting: Accounting = .income
@objc dynamic var amount = 0
@objc dynamic var date = ""
@objc dynamic var note = ""
private var privateCategory: String = Category.noCategories.rawValue
var category: Category {
get { return Category(rawValue: privateCategory)! }
set { privateCategory = newValue.rawValue }
}
}
enum Category: String {
case noCategories = "No сategories"
case food = "Food"
case cafesAndRestaurants = "Cafes And Restaurants"
case manufacturedGoods = "Manufactured Goods"
case forceMajeure = "Force Majeure"
case entertainment = "Entertainment"
}
When I try to save a property, the Сategory is saved by default("No categories")
let transactionOne = Transaction()
transactionOne.accounting = .consuption
transactionOne.amount = 250
transactionOne.category = .food
transaction privateCategory String "No categories"
As seen from all the examples in the Realm guide , string properties that you want to persist are marked with @objc dynamic
. So you should mark privateCategory
as @objc dynamic
too:
@objc dynamic var privateCategory: String = Category.noCategories.rawValue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.