2

I have a bit complicated classes in swift and Objective-C combinated together:

Keypad.h:

#import "MyApp-Swift.h" 

@interface Keypad : UIViewController {
   ...
   SwiftViewController *swiftViewController; // this is written in swift
   ...
}

This worked well.

Then I created a new swift file:

AnotherSwiftViewController.swift

 @objc class AnotherSwiftViewController: UITableViewController {
    func myMethod() {
      let keypad = appDelegate.getTabs().selectedViewController as! Keypad // I need get ObjC Keypad class
    }
}

And I need to use there the ObjcC Keypad class.

So I added it to the MyApp-Bridging-Header.h:

MyApp-Bridging-Header.h

...
// lot of other Obj-C files imported
...
#import "Keypad.h"
...

And I get the error:

> .../MyApp-Bridging-Header.h:31:9: note: in file included from .../MyApp-Bridging-Header.h:31: #import "Keypad.h"

> .../Keypad.h:13:9: error: 'My_App-Swift.h' file not found \#import "My_App-Swift.h"

> <unknown>:0: error: failed to import bridging header '.../MyApp-Bridging-Header.h'

Any ideas?

/// EDIT:

Maybe will help:

I'm using

#import "My_App-Swift.h"

In the Keypad.h file, not in standard Keypad.m, because I have there that SwiftViewController *swiftViewController; property

Maybe it will help

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
David
  • 825
  • 10
  • 33
  • Full example to use obj-c and swift together: http://stackoverflow.com/a/41068740/4488252 – Vasily Bodnarchuk Apr 03 '17 at 10:49
  • Thank you, but it doesn't help - there is not described problem which I have - calling use ObjcC in Swift which using another Swift as class variable – David Apr 03 '17 at 11:18

1 Answers1

0

You have a good point in your ///Edit.

In Keypad.h remove #import "MyApp-Swift.h" and add a forward declaration of your Swift class as follows:

...
@class SwiftViewController;
@interface Keypad : UIViewController {
...

This should do it assuming Keytab.h references SwiftViewController only by pointer and your bridging header is imported correctly otherwise.

See section Referencing a Swift Class or Protocol in an Objective-C Header in https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html.

Anatoli P
  • 4,791
  • 1
  • 18
  • 22