Swift to Objective-C header does not contain Swift classes

前端 未结 8 2067
無奈伤痛
無奈伤痛 2020-12-16 12:18

I\'m attempting to use Swift classes in my Objective-C code, however my Swift classes don\'t seem to appear in the generated header. As a result, my build fails with \"Use o

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 13:03

    Here's how I have gotten it to work. You can see a more large-scale answer here.

    Change this:

    class HelloWorld {    
        func hello() {
            println("hello world")
        }
    }
    

    To:

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

    Then, In your ObjC file:

    #import "TestApp-Swift.h"
    

    And call like this:

    HelloWorld * helloWorld = [HelloWorld newInstance];
    [helloWorld hello];
    

提交回复
热议问题