2

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.

Frank Conry
  • 2,694
  • 3
  • 29
  • 35
  • You could either declare db as `Connection?` or `Connection!`. Either way, what are you going to do if the connection fails? – Michael Feb 12 '16 at 05:11
  • 1
    that worked, sorry late night. If you write it up as an answer I'll accept. FYI I cahanged the whole do catch to `self.db = try! Connection(dbPath)` – Frank Conry Feb 12 '16 at 14:56

0 Answers0