The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Monica_G (28) [Avatar] Offline
Hello Craig,

I am in the SQLite section of Chapter 11 and I am getting a compiler error stating:

Initializer for conditional binding must have Optional type, not 'FMDatabase'

on the line
guard let db = FMDatabase(path: booksFile.path) else {

in the getOpenDB() function.

I am wondering if something has changed in the latest version of fmdb which causes the code to no longer compile. Pulling the Chapter11.6.StoreDataSQL branch it compiles fine, but it has older versions of the fmdb files.

The FMDB readme on github has the following example for creating a db connection in Swift:

let database = FMDatabase(url: fileURL)

guard else {
    print("Unable to open database")

I've modified my project to use that code (but with the path rather the url argument) and it compiles.


Craig Grummitt (50) [Avatar] Offline
Hi Monica,

Thanks again for your feedback. You're great at finding these.

It seems as though a little while back when FMDB updated to the latest version (you can read their summary of changes in version 2.7 here) they decided to make the FMDatabase(path: ) method return a non-optional.

So, as you've noted, in this code:

guard let db = FMDatabase(path: booksFile.path) else {
            print("unable to create database")
            return nil

It's now unnecessary to unwrap the database, so this can simply be replaced with:

let db = FMDatabase(path: booksFile.path)

In investigating this, I've discovered another change to the FMDB API by the way. The lastInsertRowId() method has been converted to a property. (You'll see this on page 321 of the book)

So the code which reads: = Int(db.lastInsertRowId())

Can be replaced with: = Int(db.lastInsertRowId)

I've updated this in the errata as well (at least I've requested it be updated, should be updated soon)

Thanks again!
Monica_G (28) [Avatar] Offline
Awesome! Thank you!