I have created an ObservableObject
object class, and I need to create 2 inits one with passing a string parameter and another without passing.
class DictionaryModel: ObservableObject {
@Published var dbName: String = ""
private var dbQueue: DatabaseQueue!
var localDBFile: String
//this `init` works fine:
init(_ localDBName: String) {
self.localDBFile = localDBName
var config = Configuration()
config.readonly = false
guard let dbPath = Bundle.main.path(forResource: localDBName, ofType: "sqlite") else { return }
do {
self.dbQueue = try DatabaseQueue(path: dbPath, configuration: config)
} catch let error {
print(error.localizedDescription)
}
}
}
for the second init I need to change @Published var dbName
on different occasions, for example when user choose another database. I need this changing dbName
to affects on all views like bookmark, searching and etc..
init() {
var config = Configuration()
config.readonly = false
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let dbPath = documentDirectoryURL.appendingPathComponent("\(dbName).sqlite").path
do {
self.dbQueue = try DatabaseQueue(path: dbPath, configuration: config)
} catch let error {
print(error.localizedDescription)
}
fetchBookmarks()
}
I keep getting this error self' used in property access 'dbName' before all stored properties are initialized
. Is there any way to have two inits in one class one with passing parameter and another without any?