Swift Bridging Header - Use of undeclared type 'FMDatabase' error
Asked Answered
S

4

10

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.

Shavian answered 5/8, 2015 at 16:15 Comment(3)
Have you tried an explicit import statement? Also, is DBUtil added to the same target? Just a couple thoughts...Laughing
@RomanSausarnes Thank you for your comment. I did try to import but still cannot find what to import. Also note I didn't need to import anything to access it from AppDelegate or ViewController. And Yes DBUtil is in the same project as the AppDelegate and ViewController, so it should be the same target, unless I missed something.Shavian
Files can have different targets even if they are in the same project. Look through the inspector tabs in the right hand slidable panel for some checkboxes indicating which targets it has been added to, just to be sure. That would definitely explain the behavior you are seeing.Laughing
C
17

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

Crowboot answered 5/8, 2015 at 16:51 Comment(3)
Rob: Spot on!!!! And many thanks. The lesson: bridging headers must be added to test targets (when appropriate of course), but something to keep in mind.Shavian
Number 2 solved my issue with Swift not recognizing obj-CBowhead
Unbelievable I had the very same issue. My DatabaseHelper.swift file has 2 different Target Memberships, one is unintentional. All the time I was thinking I may have missed something in my Build Settings :/Gardenia
U
1

I leave here a reply for other cases: I got the same error, but the problem was in my bridging header I forgot to write #import "FMDatabase.h"

Underwrite answered 14/3, 2016 at 13:21 Comment(0)
D
1

If you installed FMDB with CocoaPods, just add

import FMDB

in the swift file where you're using FMDatabase..

Develop answered 7/12, 2017 at 12:19 Comment(0)
D
0

My case was the same error. I fixed it by adding only the .h and .m files (not the folder) to project by ticking 'copy items when needed' && create groups and made it . Then created obj c bridging header.

On Target's Build Phases-Link Binary with Libraries- Added libsqlite3.tbd. Turned embed content contains swift code to Yes.

It solved this reference issue.

Dilan answered 28/4, 2016 at 11:35 Comment(1)
Hi,George, I have a similar trouble which is AMSmoothAlert(which is installed in Pods), so how can I let it be used in swift?Strengthen

© 2022 - 2024 — McMap. All rights reserved.