在Objective-C类中导入Swift协议 [英] Importing a Swift protocol in Objective-C class

查看:89
本文介绍了在Objective-C类中导入Swift协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将名为AnalyticProtocol的Swift协议导入名为AnalyticFactory的Objective-C类.

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

protocol AnalyticProtocol
{

}

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

在我的Swift文件中,我包含了一个名为MyProjectName-Swift.h.h文件,但是编译器向我返回一条错误消息,告诉我该文件不存在. 因此,我创建了一个名为MyProjectName-Swift.h.h文件,该文件实际上是空的(我不知道该放在里面).

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

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?

当我尝试编译时,出现以下错误::0:错误:xxxAnalyticFactory.h:39:找不到"AnalyticProtocol"的协议声明

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

以及所包含的代码:

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

我认为我不太了解如何将Swift协议导入到Objective-C类中.

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

有人在我做的事情中看到错误吗?

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

推荐答案

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

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

@objc protocol AnalyticProtocol {

}

这篇关于在Objective-C类中导入Swift协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆