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.

Jack G.
  • 3,681
  • 4
  • 20
  • 24
  • Have you tried an explicit import statement? Also, is DBUtil added to the same target? Just a couple thoughts... – Aaron Rasmussen Aug 05 '15 at 16:32
  • @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. – Jack G. Aug 05 '15 at 16:50
  • 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. – Aaron Rasmussen Aug 05 '15 at 16:57

4 Answers4

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

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    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. – Jack G. Aug 05 '15 at 17:17
  • 1
    Number 2 solved my issue with Swift not recognizing obj-C – SwiftMatt Jan 08 '16 at 11:03
  • 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 :/ – Motoko Apr 18 '16 at 05:03
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"

1

If you installed FMDB with CocoaPods, just add

import FMDB

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

balazs630
  • 3,421
  • 29
  • 46
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.

Alvin George
  • 14,148
  • 92
  • 64
  • 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? – aircraft Nov 09 '16 at 06:30