Cannot find interface declaration in myproject-swift.h - objective-c

I'
m using XLPagerTabStrip pod in my project,
i have a bridging header for other purposes to integrate from swift to objective c myproject-swift.h
i cant build the project and this error always pops:
Cannot find interface declaration for
'ButtonBarPagerTabStripViewController', superclass of
'ParentViewController'
This is my Controller
import Foundation
import UIKit
import XLPagerTabStrip
class ParentViewController: ButtonBarPagerTabStripViewController {
override func viewDidLoad() {
tabStripStyle()
super.viewDidLoad()
containerView.isScrollEnabled = false
}
}
I have seen this issue everywhere posted but its not yet answered here: 'Cannot find interface declaration' in auto-generated Swift bridging header
Bugs in swift SR-805 SR-5398

You needed to import the -Swift.h for for both the framework and the app target
For Example :
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import "XLPagerTabStrip-Swift.h"
#import "RealmSwift-Swift.h"
...... // Add all frameworks, subclasses, and dependance ios frameworks
#import "MyProject-Swift.h"
You can read this article How to import file header and check paths

I had error "Cannot find interface declaration for 'CLLocation', superclass of 'MYLocation'
for below code
#interface MYLocation : CLLocation // code in MyProject-Swift.h
when I just imported
#import <MyProject/MyProject-Swift.h>
after importing below both, the error is gone.
#import <CoreLocation/CoreLocation.h>
#import <MyProject/MyProject-Swift.h>

Related

Strange issue 'Cannot find type 'RCTResponseSenderBlock' in scope' on Swift module React Native

I have a very strange problem: "Can't find the type RCTResponseSenderBlock in scope." It's strange, because in one case it is there and in the other it doesn't, looks like a bug.
This is screenshot issue.
This my Objective-C file:
#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"
#import <React/RCTEventEmitter.h>
#import <React/RCTConvert.h>
#interface RCT_EXTERN_MODULE(BLEclass, NSObject)
RCT_EXTERN_METHOD(addEvent:(NSString *)name callback:(RCTResponseSenderBlock)callback )
#end
And this my Swift file, where i have issue :
import Foundation
#objc(BLEclass)
class BLEclass: NSObject{
#objc(addEvent:callback:)
func addEvent(_ name: String,_ callback:RCTResponseSenderBlock){
NSLog("%#", name);
let resultsDict = [
"name" : name
];
callback([NSNull(),resultsDict])
}
}
If you know how to fix it, please write answer.
Thanks.
I had this same issue and discovered I had to add the import to my main bridge file.
#import <React/RCTBridgeModule.h>
#import <React/RCTViewManager.h>
I out which file was set by looking at the Build settings under Swift Compiler - General -> Objective-C Bridging Header which appears to be the default for the app.

Combinating Swift & Objective C files

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

How to make protocol from Swift visible in .h file?

I have a 3 files:
PLThreadViewController.swift
#objc protocol PLThreadViewDelegate {
func threadViewControlledWillDismiss(threadViewController: PLThreadViewController)
}
class PLThreadViewController: UIViewController {}
PLMessagesTableViewController.h
#import <UIKit/UIKit.h>
#interface PLMessagesTableViewController : UITableViewController <PLThreadViewDelegate>
#end
The problem is: Cannot find the protocol declaration for PLThreadViewDelegate
When i try to add:
#import "Module-Swift.h"
then:
Module-Swift.h file not found. The name of module is very correct. I read about spaces, underscores in the name and so on.
Did Xcode create a bridging header for you? If so, look at the naming Xcode uses there and that will clue you in on how your "-Swift.h" file is named. So if your bridging header is named My_Project-Bridging-Header.h, your auto-generated Swift header will be My_Project-Swift.h.

Swift class using Objective-C class using Swift class

I have an obj-c project to which I successfully added a new Swift class A, which is being used by some existing obj-c class B - the use of the automatically generated "MyProject-Swift.h" header worked as expected.
I also successfully added a new Swift class C that uses some existing obj-c class D - the use of the bridging header also worked as expected.
However, suppose I want to refer from my Swift class C to the existing obj-c class B (which in turn refers to the new Swift class A). In order to do that I need to import "B.h" to the bridging header. However, if I do that I get an error in class B: "'MyProject-Swift.h' file not found" (i.e., the file is no longer generated).
Am I doing something wrong or is this a kind of interaction between Swift and Objective-C that is not allowed? It looks like there is a kind of circular reference that the compiler is unable to solve.
--- EDIT ---
I'll try to make the question clearer by adding some code.
-- PREAMBLE --
I added a new Swift class to an obj-c project:
// SwiftClassA.swift
import Foundation
#objc class SwiftClassA : NSObject {
var myProperty = 0
}
The code compiles correctly and is translated into obj-c stubs in the automatically generated "MyProject-Swift.h" header like so:
// MyProject-Swift.h
...
SWIFT_CLASS("_TtC7MyProject11SwiftClassA")
#interface SwiftClassA : NSObject
#property (nonatomic) NSInteger myProperty;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
Now, one obj-c class uses SwiftClassA:
// ObjCClass.h
#import <Foundation/Foundation.h>
#import <MyProject-Swift.h>
#interface ObjCClass : NSObject
#property (nonatomic, strong) SwiftClassA *aProperty;
#property (nonatomic) int *aNumber;
#end
This also works seamlessly.
-- THE QUESTION --
Can I now create a new Swift class that refers to the obj-c class (ObjCClass) that is using the Swift class SwiftClassA?
This is what I can't do.
If I add the new Swift class:
// SwiftClassB.swift
import Foundation
#objc class SwiftClassB : NSObject {
var aPropertyOfClassB = 1
func someFunc() {
var objCObject = ObjCClass()
var theProperty = objCObject.aProperty
print("The property is \(theProperty)")
}
}
this of course won't compile because of "Use of unresolved identifier 'ObjCClass'". So I need to add that to the bridging header file:
// BridgingHeader.h
#ifndef MyProject_BridgingHeader_h
#define MyProject_BridgingHeader_h
...
#import "ObjCClass.h"
#endif
However, if I do that, the ObjCClass.h file won't compile giving a "'MyProject-Swift.h' file not found".
I've read in several places (with no example, though) that this may mean that there is a circular reference and that a forward reference using #class could solve the problem. However, I'm not sure what needs to be forward referenced and where, and all my attempts failed.
I hope the question is no longer confusing now!
This is a typical cyclical referencing problem.
Be careful to read the docs:
To avoid cyclical references, don’t import Swift into an Objective-C header file. Instead, you can forward declare a Swift class to use it in an Objective-C header. Note that you cannot subclass a Swift class in Objective-C.
So, you should use "forward declare" in .h, and #import in .m:
// ObjCClass.h
#import <Foundation/Foundation.h>
#class SwiftClassA;
#interface ObjCClass : NSObject
#property (nonatomic, strong) SwiftClassA *aProperty;
#property (nonatomic) int *aNumber;
#end
// ObjCClass.m
#import "ObjCClass.h"
#import "MyProject-Swift.h"
#implementation ObjCClass
// your code
#end

Xcode project complains about missing files if a linked framework contains private headers

My problem is this:
My framework contains public and private headers - the public headers import private headers in the framework
My app that links against this framework imports public headers
Now when I compile it, Xcode complains about missing files (the private headers that are indirectly imported via the frameworks public headers). I read somewhere on stackoverflow that I should do this:
"In the public header file use #class to include other interfaces and use #import in the implementation file (.m)."
I find this solution pretty unsatisfying - you have to use it for circular dependencies, too. Is there any better way to keep my headers private?
To get about circular references use the #class directive in the header and the #import in the source file.
In OtherClass.h:
#class MyClass;
#interface OtherClass
{
MyClass *myInstance;
}
#end
In OtherClass.m:
#import "OtherClass.h"
#import "MyClass.h"
#implement OtherClass
#end
In MyClass.h:
#class OtherClass;
#interface MyClass
{
OtherClass *otherInstance;
}
#end
In MyClass.m:
#import "MyClass.h"
#import "OtherClass.h"
#implement MyClass
#end