将swift框架导入Objective-c项目 [英] importing swift framework into a objective-c project
问题描述
我将swift框架导入Objective-c项目,如下所示:
I am importing swift framework into objective-c project like this:
@import MyFramework;
问题是我导入框架的类只能识别某些类。
The problem is that only some of the classes are recognized by the class i am importing the framework.
被识别的类:
public class RecognizedClass:UIViewController, WKNavigationDelegate, WKScriptMessageHandle
{ ... }
该类不是:
public class VeediUtils
{ ... }
他们都是公开的,那么为什么第一个在工作区被识别而另一个没有?
They are both public so why the first is recognized in the workspace and the other not?
我也看到了头文件MyFramework-Swift.h一个类
Also i see in the header file MyFramework-Swift.h that a class
@interface RecognizedClass : UIViewController <WKNavigationDelegate, WKScriptMessageHandler>
出现而另一个不出现
为什么那是吗?
还指出当我将swift框架导入swift项目时,同样的程序有效
Also to point that this same procedure work when i am importing swift framework to swift project
推荐答案
要访问objc中的swift类,这不是从NSObject继承的,你需要:
To access a swift class in objc, that is not inherited from NSObject you need to:
@objc public class VeediUtils
@objc public class VeediUtils
必须使用@objc属性标记Swift类或协议才能在Objective-C中访问和使用。该属性告诉编译器可以从Objective-C访问这段Swift代码。如果你的Swift类是Objective-C类的后代,编译器会自动为你添加@objc属性。
A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you.
这篇关于将swift框架导入Objective-c项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!