2

So I'm trying to use an @objc swift class as a property on my objective-c header file.

example

@objc class SwiftClass: NSObject {
}


@interface ObjcObject : NSObject

#pragma mark - Public Properties -
@property (nonatomic, weak) SwiftClass* swiftClass;

However if I #import ProjectName-Swift.h it gives me the error that "'ProjectName-Swift.h'file not found'

I found an answer here that implies you can't actually import Swift into a header file https://stackoverflow.com/a/29951314/3877767

How then am I supposed to use swift code together with objc code? I would not call swift and objc interoperable if you can't use @objc marked swift classes as properties on and objc header file

So inside of my App-Bridging-Header.h

#import "ObjcClass.h"

and inside of my ObjcClass.h

 #import "ProjectName-Swift.h"

This gives the error ProjectName-Swift.h can't find file

Either removing the #import ProjectName-Swift.h, or the #import ObjcClass.hfixes the problem, but then I can't use the Swift/OBJC code respectively

Community
  • 1
  • 1
YichenBman
  • 5,011
  • 8
  • 46
  • 65
  • The problem was a cyclical reference. Forward declaring worked for classes, but haven't found a solution for protocols yet – YichenBman Mar 23 '16 at 20:32

1 Answers1

0

You don't import the header file. You give it to the compiler as a so-called "bridging header" that tells the compiler what to share between the two language environments.

You need to put your header file under the Objective-C Bridging Header as shown below:

Xcode

SevenBits
  • 2,836
  • 1
  • 20
  • 33
  • I'm sorry it may not have been clear, you do import class header files within the Bridging Header file. If I import this particular class .h file in the Bridging-Header.h file, and also import the -Swift.h file, it kills it but only if both of those things happen. If I don't do one or the other it works fine – YichenBman Feb 10 '16 at 01:21