简体   繁体   中英

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

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.

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:

    在此输入图像描述

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

    在此输入图像描述

我在这里留下了其他案例的答复:我得到了同样的错误,但问题出在我的桥接标题中我忘了写#import "FMDatabase.h"

If you installed FMDB with CocoaPods, just add

import FMDB

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM