Habit model class
This represents one Habit, like Walking, Journaling, Meditating, etc.
- create a
Model
group (right click in the file navigator and selectNew Group
) - create a
Habit
model class- extend from
Object
- add
name
anddesc
properties (asString
) anddone
asBool
- extend from
/Model/Habit.swift
import RealmSwift
class Habit: Object {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name = ""
@Persisted var desc = ""
@Persisted var done = false
}
- let's add a
convenience
init
/Model/Habit.swift
convenience init(name: String = "", desc: String = "", done: Bool = false) {
self.init()
self.name = name
self.desc = desc
self.done = done
}
Complete listing
/Model/Habit.swift
import RealmSwift
class Habit: Object {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name = ""
@Persisted var desc = ""
@Persisted var done = false
convenience init(name: String, desc: String, done: Bool) {
self.init()
self.name = name
self.desc = desc
self.done = done
}
}