iOS - 在 Swift 代码中使用 Cocoapods objective-C

标签 ios xcode swift cocoapods

我正在尝试将 objective-C/CocoaPods 代码 ( GBFlatButton ) 添加到现有的 swift 代码中。我阅读了所有关于页眉设置的信息,我这样做了,让我知道是否表现良好。

Header Bridging configuration

头文件中还添加了正确的导入文件。

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import <UIKit/UIKit.h>
#import <GBFlatButton/GBFlatButton.h>
#import <GBFlatButton/UIColor+GBFlatButton.h>

另一方面,当我编译代码时,CocoaPods 项目没有给我一个错误。当我使用按钮时发生错误,可能是我尝试使用此 objective-c 代码的方式:

let MyButton: GBFlatButton! = GBFlatButton(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 40.0))

我也试过这样使用它:@IBOutlet var FButton: GBFlatButton!

这是我遇到的最后一个错误:

/Users//Documents/Projects/party/party/party/partyStepViewController.swift:18:19: Use of undeclared type 'GBFlatButton'

/Users//Documents/Projects/party/party/party/partyStepViewController.swift:18:9: Could not infer type for 'MyButton'

任何线索都会有所帮助:)

谢谢!!如果您需要更多信息,请随时问我。

最佳答案

您忘记在应用程序的build设置中添加桥接头

enter image description here

在 View Controller 中:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let button: GBFlatButton! = GBFlatButton(frame: CGRectMake(0, 0, 200, 50))
        self.view.addSubview(button)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

只要这样做就可以了

关于iOS - 在 Swift 代码中使用 Cocoapods objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738647/

相关文章:

ios - Swift:无法在可变对象参数上设置属性值

ios - 使用 RxSwift 注册两个自定义单元格

ios - 单击 "cross"按钮后无法从 super View 中删除 UIProgressView?

objective-c - 从位于自定义 UITableViewCell 中的 UITextView 获取文本

swift - 如何使 View 响应触摸(而不是点击)

objective-c - Swift 代码可以嵌入现有的 Objective-C 文件中吗?还是必须将该代码添加到离散的 Swift 文件中?

ios - 界面生成器 View ui 设计在横向模式 ios 中更改

ios - 关闭 ViewController 会导致相机延迟

swift - 在某些情况下,使用 searchController 推送过渡不会使 navigationBar 的大小变化平滑

iphone - 使用 SBJSON 进行 JSON 解析和检查