Can't use Swift classes inside Objective-C

前端 未结 25 2035
野趣味
野趣味 2020-11-22 08:10

I try to integrate Swift code in my app.My app is written in Objective-C and I added a Swift class. I\'ve done everything described he

25条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 08:33

    For Swift 5:

    1. Add the @objc keyword to your class and methods
    2. Add public keyword to your class and methods
    3. Let your class inherit from NSObject
    4. Build Project
    5. Put #import "MyProject-Swift.h" in your Objective-C file

      @objc
      public class MyClass: NSObject {
      
          @objc
          public func myMethod() {
      
          }
      }
      

提交回复
热议问题