Vladimir
Vladimir

Reputation: 7801

Swift class is not added to autogenerated header for swift in Obj-C project

When I've added Swift class to Objective-C project it was not included to autogenerated header for swift.

My steps was as following:

  1. Created single view Objective C project with name "SwiftTest"
  2. Build Setting -> Packaging -> Defines Module set to "YES"
  3. Product Name (and Product Module Name) was not changes so it was "SwiftTest"
  4. Added Swift file SwiftInObjc.swift with single class:

    import Foundation
    class SwiftInObjc : NSObject{
        func printHello(){
            println("Hello")
        }
    }
  1. In ViewController class included header and declared swift object:

#import "SwiftTest-Swift.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    SwiftInObjc * swiftObj;
}

After that project can't be build because of error:

Use of undeclared identifier "SwiftInObjc"

Header file "SwiftTest-Swift.h" has been created in derived data as expected, but it doesn't contain my swift class.

Note: My case is similar to this question but my class was subclassed from NSObject so it must be another reason. (also I've tried pure Swift class:


import Foundation

@objc class SwiftInObjc {
    class func newInstance() -> SwiftInObjc {
        return SwiftInObjc()
    }
    func hello() {
        println("hello")
    }
}

but it doesn't help, I still have same error)

Upvotes: 2

Views: 559

Answers (1)

Ievgen
Ievgen

Reputation: 1596

Try to declare

@objc public class SwiftInObjc {}

Upvotes: 1

Related Questions