2

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)

Community
  • 1
  • 1
Vladimir
  • 7,670
  • 8
  • 28
  • 42
  • 1
    I just followed your exact steps and all I got was the expected warning about swiftObj being an unused variable. It compiled and built just fine. I'm using Xcode 6.1; presumably you're also on the current release? – Matt Gibson Nov 13 '14 at 17:40
  • Issue closed. Matt was right, it was XCode version issue. But he did not post answer so I can't accept it. – Vladimir Mar 30 '15 at 08:32
  • Making sure the swift class inherits from NSObject did it for me. – zekel Feb 09 '16 at 15:43

1 Answers1

1

Try to declare

@objc public class SwiftInObjc {}
Ievgen
  • 1,596
  • 16
  • 13