Importing a Swift protocol in Objective-C class

后端 未结 5 1842
耶瑟儿~
耶瑟儿~ 2020-12-23 00:11

I try to import a Swift Protocol named AnalyticProtocol into an Objective-C class named AnalyticFactory.

protocol AnalyticProtocol
         


        
相关标签:
5条回答
  • 2020-12-23 00:50

    You need to add the @objc attribute to your Swift protocol like so:

    @objc protocol AnalyticProtocol {
    
    }
    
    0 讨论(0)
  • 2020-12-23 00:50

    If your are creating a framework, the required import

    #import "ProductModuleName-Swift.h"
    

    changes to:

    #import <ProductModuleName/ProductModuleName-Swift.h>
    

    In this case you also must make the swift protocol public:

    @objc public protocol AnalyticProtocol {
      func something();
    }
    
    0 讨论(0)
  • 2020-12-23 00:53

    For anybody who simply needs to adopt a protocol – you can do this in two steps, without generating any warnings or errors:

    1. In your .swift file, add @objc before the protocol name:

      @objc protocol AnalyticProtocol {
      
      }
      
    2. In your .m file, import the generated Swift header and adopt the protocol in a private category. (The header file is named automagically):

      #import "ProductModuleName-Swift.h"
      
      @interface AnalyticFactory () <AnalyticProtocol>
      
      @end
      

    This is Apple’s recommended approach. You can learn more about mixing and matching Objective-C and Swift here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

    0 讨论(0)
  • 2020-12-23 00:59

    We can use swift protocols in Objective C with few changes to the code. Also, @objc declared protocols let you have optional and required methods without default implementations. It comes with pros and cons.

    We could actually re-name the protocol name to a more descriptive when using in Objective C. I make use of "@objc(alias_name)".

    Here is the code, Let's have a swift protocol with @objc attribute and alias name to use in the ObjC code.

    @objc(ObjTableViewReloadable) protocol TableViewReloadable: class {
       func reloadRow(at index: IndexPath)
       func reloadSection(at index: Int)
       func reloadTable()
    }
    

    Now lets farword declare our protocol in .h file

    @protocol ObjTableViewReloadable;
    

    You can now conform to this protocol in .m file and add required methods implementation.

    #import "MyApp-Swift.h"
    @interface MyObjcViewController () <ObjTableViewReloadable>
    
    0 讨论(0)
  • 2020-12-23 01:06

    It is not possible to import the Xcode generated Swift header in objC header files.

    So, since you want to use Swift code in an objC header file, you will need to "forward declare" the classes and protocols you want to use in the objC header file, like this:

    @protocol AnalyticProtocol;
    

    You can now use the protocol in your objC class declaration:

    @interface AnalyticFactory : NSObject
    {
        Class<AnalyticProtocol> _analyticProtocolClass; // The type of the analytic class currently used.
    }
    

    In your implementation file (the objC .m file), you can import the Xcode generated Swift header ("ProductModuleName-Swift.h") file and the correct implementation AnalyticProtocol will now be known to the compiler.

    This is also described in the section "Using Swift from Objective-C" in the Apple Docs

    Note that XCode will give a warning in the objC header file when you use the forward declared protocol ("Cannot find protocol definition for 'AnalyticProtocol'), but this is can be ignored - the implementation will be found at compile time.

    0 讨论(0)
提交回复
热议问题