Leonardo Heis
Leonardo Heis

Reputation: 109

iOS - Using Cocoapods objective-C within Swift Code

I'm trying to add an objective-C/CocoaPods code (GBFlatButton) to an existing swift code. I read all about I found regarding Header settings and I did this, let me know if is well performed.

Header Bridging configuration

Also the Header file has the right import files added to it.

//
//  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>

On the other hand the CocoaPods project doesn't give me an error when I compile the code. The error occurs when I use the button, maybe is the way I'm trying to use this objective-c code:

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

Also I tried to use it like this: @IBOutlet var FButton: GBFlatButton!

This is the final error I have:

/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'

Any clue will help :)

Thanks!!, if you need more info don't hesitate to ask me.

Upvotes: 1

Views: 181

Answers (1)

Himanshu gupta
Himanshu gupta

Reputation: 650

you forgot to add the bridging header in the Build settings of the app

enter image description here

In ViewController:

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.
    }
}

just do this and then it will work

Upvotes: 3

Related Questions