Search code examples
objective-cswiftobjective-c-swift-bridge

Can I prevent extensions from being exported to Swift header for Objective-C?


I have an Objective-C project with 1 Swift class. This class is using a framework that is also written in Swift. (Used CocoaPods to include the framework)

My problem is that the -Swift.h file is exporting my extensions that adhere to protocols in the framework. Now when I try to import the -Swift.h file in the Objective-C, it complains that that the protocol definitions cannot be found.

I don't want these extensions exported. They are only used in this class. I can't use private or fileprivate for extensions that declare protocol conformances. I also tried adding @nonobjc before the extension declaration (which cascaded warnings into my methods) and it was still exported.

Here are my extensions:

extension MessagingExperience: MessagingDelegate {
    ...
}

extension MessagingExperience: MessagingNotificationDelegate {
    ...
}

And generated header:

@interface MessagingExperience (SWIFT_EXTENSION(Reference_App)) <MessagingDelegate>
- (void)MessagingObseleteVersion:(NSError * _Nonnull)error;
- (void)MessagingError:(NSError * _Nonnull)error;
@end


@interface MessagingExperience (SWIFT_EXTENSION(Reference_App)) <MessagingNotificationDelegate>
- (BOOL)shouldShowMessagingNotificationWithNotification:(MessagingNotification * _Nonnull)notification SWIFT_WARN_UNUSED_RESULT;
- (void)messagingNotificationTapped:(MessagingNotification * _Nonnull)notification;
- (UIView * _Nonnull)customMessagingNotificationViewWithNotification:(MessagingNotification * _Nonnull)notification SWIFT_WARN_UNUSED_RESULT;
@end

Errors produced by including the -Swift.h in an Objective-C class:

Cannot find protocol declaration for 'MessagingDelegate'

Cannot find protocol declaration for 'MessagingNotificationDelegate'

Is there a way to prevent this from being in the header?

Thanks.


Solution

  • Create a private class in MessagingExperience.swift that conforms to the protocol defined in the framework. Because it's a private class, the protocol won't be specified in the generated -Swift.h.