Swift Bridging Header - Use of undeclared type 'FMDatabase' error

Jack G. picture Jack G. · Aug 5, 2015 · Viewed 13.8k times · Source

I have looked at all the other posts with the same error (use of undeclared type) but still can’t figure out what is wrong with my project.

The difference with the other cases is that I can successfully use the FMDatabase in AppDelegate and ViewController classes but not from another class I've created, though in the same project as the AppDelegate and ViewController classes.

And by "successfully use", I mean I can access the database and tables in it.

Also note that I didn’t have to import anything to use FMDatabase in AppDelegate or ViewController.

So far what I have done (Xcode. 6.4 Swift 1.2):

  1. Created a single view swift project.

  2. Installed FMDB using cocoapods (https://cocoapods.org/?q=fmdb)

  3. Created a bridging header for FMDB

I can successfully declare and use FMDatabase from the AppDelegate.swift and ViewController.swift classes.

AppDelegate.swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var database: FMDatabase? // OK
    ...

ViewController.swift

class ViewController: UIViewController {

    var database: FMDatabase? // OK
    ...

In DBUtil.swift though I’m getting the “Use of undeclared type ‘FMDatabase’” error.

DBUtil.swift

class DBUtil {

    var database: FMDatabase? // Error: "Use of undeclared type 'FMDatabase'

}

A reproducer is available at Swift Bridging Header.

For any hints or ideas, thank you in advance.

Answer

Rob picture Rob · Aug 5, 2015

It's because DBUtil is part of the BridgingHeaderTests target (but the app delegate and the view controller are not), but that target isn't configured for FMDB (the "Objective-C Bridging Header" setting for the test target is empty).

You can either:

  1. Remove DBUtil from the tests target:

    enter image description here

  2. Or specify the bridging header for the tests target:

    enter image description here