Can't access objective-c method from Swift.

I have an ojb-c method.


- (instancetype)initWithInitialFilter:(nullable ArticleFilter *)filter
                      channelProvider:(id)channelProvider
                  offlineContentManager:(OfflineContentManager *)offlineContentManager;


The swift interface is generated as such:


public convenience init(initialFilter filter: ArticleFilter?, channelProvider: ChannelProviderProtocol, offlineContentManager: OfflineContentManager)


The objc-c header in question is listed my bridging header (all are listed). However, when I try to access the above initializer from swift, it's not visible. Compiler says nothing see too see there! There's also no method in autocompletion. Tips, cause I'm flummoxed.

I cannot reproduce the same result as you described.


First, I could not make the initializer exposed to the generated interface. Of course, you cannot access it from Swift.


Second, when I modified the .h file and succeeded to make the method shown in the generated interface, I could access if from Swift, listed in autocompletion.


Can you create a minimized project which can reproduce the same issue, and show whole codes? All header files and Swift code.

For anyone still stuck in a similar situation, I was able to reproduce the scenario.

Code Block objc
- (instancetype)initWithInitialFilter:(nullable ArticleFilter *)filter
channelProvider:(id)channelProvider
offlineContentManager:(OfflineContentManager *)offlineContentManager;


The OP mentions the following:

The objc-c header in question is listed my bridging header (all are listed)

In such an instance, all classes viz. ArticleFilter, OfflineContentManager also need to be in the bridging header.

Assume that ArticleFilter is implemented in Swift. In such a case, the method would not show up. Importing the "Module-Swift.h" causes a cyclic linking issue so you'd make a class definition in that header using @class ArticleFilter so the method signature continues to make sense.

The only way I've gotten it to work without breaking anything is to convert the method to something like:

Code Block objc
- (instancetype)initWithInitialFilter:(id)filter
channelProvider:(id)channelProvider
offlineContentManager:(OfflineContentManager *)offlineContentManager;


The method change only needs to happen in the header file. No changes are required in the implementation file. It isn't optimum, makes things confusing sometimes, but it works. I hope we get a better solution in the near future.



Can't access objective-c method from Swift.
 
 
Q