我可以阻止扩展导出到Objective-C的Swift头吗?

时间:2022-09-07 07:45:29

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)

我有一个带有1个Swift类的Objective-C项目。这个类使用的是一个也用Swift编写的框架。 (使用CocoaPods来包含框架)

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.

我的问题是-Swift.h文件正在导出我的扩展,这些扩展遵循框架中的协议。现在,当我尝试在Objective-C中导入-Swift.h文件时,它抱怨无法找到协议定义。

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.

我不希望导出这些扩展名。它们仅用于本课程。我不能使用private或fileprivate来声明协议一致性的扩展。我还尝试在扩展声明之前添加@nonobjc(将警告级联到我的方法中)并且它仍然被导出。

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:

通过在Objective-C类中包含-Swift.h而产生的错误:

Cannot find protocol declaration for 'MessagingDelegate'

找不到'MessagingDelegate'的协议声明

Cannot find protocol declaration for 'MessagingNotificationDelegate'

找不到'MessagingNotificationDelegate'的协议声明

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

有没有办法防止这个在标题中?

Thanks.

1 个解决方案

#1


0  

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.

在MessagingExperience.swift中创建一个符合框架中定义的协议的私有类。因为它是私有类,所以不会在生成的-Swift.h中指定协议。

#1


0  

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.

在MessagingExperience.swift中创建一个符合框架中定义的协议的私有类。因为它是私有类,所以不会在生成的-Swift.h中指定协议。