在Objective-C类中导入Swift协议。

时间:2023-01-15 17:47:52

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

我试图将一个名为AnalyticProtocol的Swift协议导入到一个名为AnalyticFactory的Objective-C类中。

protocol AnalyticProtocol
{

}

I'm starting from an existing Objective-C project (I didn't create a new Swift project with xCode and I didn't found how configure my Objective-C project to be a Swift project in xCode 6).

我从一个现有的Objective-C项目开始(我没有使用xCode创建一个新的Swift项目,也没有发现如何将我的Objective-C项目配置成xCode 6中的Swift项目)。

In my Swift file I included the .h file named MyProjectName-Swift.h but the compiler return me an error telling me that it doesn't exist. So, I created a .h file named MyProjectName-Swift.h which is actually empty (I don't know what I should put inside).

在我的Swift文件中,我包含了名为MyProjectName-Swift的.h文件。但是编译器会返回一个错误告诉我它不存在。因此,我创建了一个名为MyProjectName-Swift的.h文件。h实际上是空的(我不知道应该放什么进去)。

In the Apple documentation they said that I have to include my .h file named MyProjectName-Swift.h in my .m file. But I need to include it not into my .mfile but into my .h. Does this can be problematic?

在苹果文档中,他们说我必须把我的。h文件命名为MyProjectName-Swift。h在我的。m文件中。但我需要将它包含在。mfile中,而不是写入。h。这会有问题吗?

When I try to compile I've got this error: :0: error: xxxAnalyticFactory.h:39: cannot find protocol declaration for 'AnalyticProtocol'

当我试图编译时,我得到了这个错误::0:错误:xxxAnalyticFactory。h:39:不能找到“分析协议”的协议声明

And the incriminated code:

和不道德的代码:

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

I think I don't understand well how can I import a Swift protocol into an Objective-C class.

我想我不太明白如何将一个Swift协议导入到Objective-C类中。

Does anyone see an error in what I'm doing?

有人看到我正在做的错误吗?

3 个解决方案

#1


167  

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

您需要将@objc属性添加到您的Swift协议中:

@objc protocol AnalyticProtocol {

}

#2


54  

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

在objC头文件中导入Xcode是不可能的。

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:

因此,由于您希望在objC头文件中使用Swift代码,因此您需要“转发”您希望在objC头文件中使用的类和协议,例如:

@protocol AnalyticProtocol;

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

您现在可以在objC类声明中使用该协议:

@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.

在您的实现文件(objC .m文件)中,您可以导入Xcode生成的Swift头文件(“ProductModuleName-Swift.h”)文件和正确的实现分析协议现在将被编译器知道。

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

在苹果文档中,“使用Objective-C的Swift代码”也描述了这一点。

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.

注意,当您使用forward声明的协议(“不能找到‘AnalyticProtocol’的协议定义”)时,XCode将在objC头文件中发出警告,但这是可以忽略的——在编译时将会找到实现。

#3


18  

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:

    在您的.swift文件中,在协议名称之前添加@objc:

    @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):

    在您的.m文件中,导入生成的Swift头,并在私有类别中采用该协议。(头文件被自动命名):

    #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

这是苹果推荐的方法。你可以在这里学到更多关于混合和匹配Objective-C和Swift的知识:https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html。

#1


167  

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

您需要将@objc属性添加到您的Swift协议中:

@objc protocol AnalyticProtocol {

}

#2


54  

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

在objC头文件中导入Xcode是不可能的。

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:

因此,由于您希望在objC头文件中使用Swift代码,因此您需要“转发”您希望在objC头文件中使用的类和协议,例如:

@protocol AnalyticProtocol;

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

您现在可以在objC类声明中使用该协议:

@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.

在您的实现文件(objC .m文件)中,您可以导入Xcode生成的Swift头文件(“ProductModuleName-Swift.h”)文件和正确的实现分析协议现在将被编译器知道。

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

在苹果文档中,“使用Objective-C的Swift代码”也描述了这一点。

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.

注意,当您使用forward声明的协议(“不能找到‘AnalyticProtocol’的协议定义”)时,XCode将在objC头文件中发出警告,但这是可以忽略的——在编译时将会找到实现。

#3


18  

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:

    在您的.swift文件中,在协议名称之前添加@objc:

    @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):

    在您的.m文件中,导入生成的Swift头,并在私有类别中采用该协议。(头文件被自动命名):

    #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

这是苹果推荐的方法。你可以在这里学到更多关于混合和匹配Objective-C和Swift的知识:https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html。