xcode Swift Bridging Header - 使用未声明的类型“FMDatabase”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31837751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:37:42  来源:igfitidea点击:

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

iosobjective-cxcodeswiftxcode6.4

提问by Hyman G.

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.

与其他情况的不同之处在于,我可以在 AppDelegate 和 ViewController 类中成功使用 FMDatabase,但不能从我创建的另一个类中成功使用 FMDatabase,尽管与 AppDelegate 和 ViewController 类在同一个项目中。

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.

另请注意,我无需导入任何内容即可在 AppDelegate 或 ViewController 中使用 FMDatabase。

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

到目前为止我所做的(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

  1. 创建了一个单视图 swift 项目。

  2. 使用 cocoapods 安装 FMDB ( https://cocoapods.org/?q=fmdb)

  3. 为 FMDB 创建桥接头

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

我可以从 AppDelegate.swift 和 ViewController.swift 类中成功声明和使用 FMDatabase。

AppDelegate.swift

AppDelegate.swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var database: FMDatabase? // OK
    ...

ViewController.swift

视图控制器.swift

class ViewController: UIViewController {

    var database: FMDatabase? // OK
    ...

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

在 DBUtil.swift 中,虽然我收到了“使用未声明的类型 'FMDatabase'”错误。

DBUtil.swift

DBUtil.swift

class DBUtil {

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

}

A reproducer is available at Swift Bridging Header.

复制器可在Swift Bridging Header 获得

For any hints or ideas, thank you in advance.

对于任何提示或想法,提前致谢。

回答by Rob

It's because DBUtilis part of the BridgingHeaderTeststarget (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).

这是因为DBUtilBridgingHeaderTests目标的一部分(但应用程序委托和视图控制器不是),但该目标未配置为 FMDB(测试目标的“Objective-C Bridging Header”设置为空)。

You can either:

您可以:

  1. Remove DBUtilfrom the tests target:

    enter image description here

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

    enter image description here

  1. DBUtil从测试目标中删除:

    在此处输入图片说明

  2. 或者为测试目标指定桥接头:

    在此处输入图片说明

回答by balazs630

If you installed FMDB with CocoaPods, just add

如果你用 CocoaPods 安装了 FMDB,只需添加

import FMDB

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

在您使用 FMDatabase 的 swift 文件中..

回答by balazs630

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"

我在这里留下了其他情况的回复:我遇到了同样的错误,但问题出在我忘记写的桥接头中 #import "FMDatabase.h"

回答by A.G

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.

我的情况是同样的错误。我通过勾选“需要时复制项目”&& 创建组,仅将 .h 和 .m 文件(而不是文件夹)添加到项目中来修复它。然后创建了 obj c 桥接头。

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

在 Target 的构建阶段-使用库链接二进制文件-添加了 libsqlite3.tbd。将包含 swift 代码的嵌入内容变为是。

It solved this reference issue.

它解决了这个参考问题。