I am trying to create a class to handle all the database interaction based on SQLite.swift
and keep running into errors because the initialization of the the connection (and other related objects) happens in a try catch. Here is a simplified example to illustrate the problem:
class DBUtil {
var db : Connection
let settings = Table("settings")
let key = Expression<String>("key")
let val = Expression<String>("val")
init() {
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true
).first!
let dbPath = "\(path)/geonames.sqlite3"
do {
self.db = try Connection(dbPath)
try db.run(self.settings.create(ifNotExists: true) { t in
t.column(key, unique: true)
t.column(val)
})
} catch _ {
}
}
func updateUserId(userId : String) {
do {
/* INSERT */
let insert = settings.insert(key <- "user", self.val <- userId)
let _ = try self.db.run(insert)
} catch _ {
}
}
}
But xcode shows me the following error: Return from initializer without initializing all stored properties
not sure how I could satisfy this when I have to initialize the db connection in a try catch like this.