将swift框架导入到objective-c项目中。

时间:2022-09-07 08:30:26

I am importing swift framework into objective-c project like this:

我将swift框架导入objective-c项目如下:

@import MyFramework;

The problem is that only some of the classes are recognized by the class i am importing the framework.

问题是,只有一些类被我导入框架的类识别。

The class which is recognized:

被认可的类别:

public class RecognizedClass:UIViewController, WKNavigationDelegate, WKScriptMessageHandle 
 { ... } 

The class which is not:

非:

public class VeediUtils
{ ... } 

They are both public so why the first is recognized in the workspace and the other not?

它们都是公共的,那么为什么第一个在工作区中被识别,而另一个在工作区中不被识别?

Also i see in the header file MyFramework-Swift.h that a class

我还在头文件myframeworkswift中看到。h是一个类

@interface RecognizedClass : UIViewController <WKNavigationDelegate, WKScriptMessageHandler>

Appear while the other dont

出现而另一个不出现

Why is that?

这是为什么呢?

Also to point that this same procedure work when i am importing swift framework to swift project

同样,当我导入swift框架到swift项目时,同样的过程也适用。

3 个解决方案

#1


18  

To access a swift class in objc, that is not inherited from NSObject you need to:

要访问objc中的swift类,不从NSObject继承,您需要:

@objc public class VeediUtils

@objc公开课VeediUtils

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类或协议必须使用@objc属性进行标记,以便在Objective-C中可以访问和使用。这个属性告诉编译器可以从Objective-C访问这段Swift代码。如果您的Swift类是Objective-C类的后代,编译器会自动为您添加@objc属性。

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

#2


23  

If you previously configured Project for integrating with Swift and want to use Swift Dynamic Framework, you have to import it like this way (in { } you have to put appropriate names depends on your Project):

如果您之前配置了与Swift集成的项目,并且想要使用Swift动态框架,您必须这样导入它(在{}中,您必须根据您的项目设置适当的名称):

#import <{MyFramework}/{MyFrameworkMainClass}-Swift.h>  
#import "{YourProjectTargetName}-Swift.h"

#3


4  

You have to add @objc to the declaration of the VeediUtils class, or make it inherit from NSObject. Otherwise it won't be visible to Objective-C.

您必须将@objc添加到VeediUtils类的声明中,或者使它从NSObject继承。否则它对Objective-C不可见。

In your case, RecognizedClass is recognized because it is a subclass of UIViewController, which is a subclass of NSObject.

在你的例子中,被识别的类因为它是UIViewController的子类,它是NSObject的子类。

#1


18  

To access a swift class in objc, that is not inherited from NSObject you need to:

要访问objc中的swift类,不从NSObject继承,您需要:

@objc public class VeediUtils

@objc公开课VeediUtils

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类或协议必须使用@objc属性进行标记,以便在Objective-C中可以访问和使用。这个属性告诉编译器可以从Objective-C访问这段Swift代码。如果您的Swift类是Objective-C类的后代,编译器会自动为您添加@objc属性。

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

#2


23  

If you previously configured Project for integrating with Swift and want to use Swift Dynamic Framework, you have to import it like this way (in { } you have to put appropriate names depends on your Project):

如果您之前配置了与Swift集成的项目,并且想要使用Swift动态框架,您必须这样导入它(在{}中,您必须根据您的项目设置适当的名称):

#import <{MyFramework}/{MyFrameworkMainClass}-Swift.h>  
#import "{YourProjectTargetName}-Swift.h"

#3


4  

You have to add @objc to the declaration of the VeediUtils class, or make it inherit from NSObject. Otherwise it won't be visible to Objective-C.

您必须将@objc添加到VeediUtils类的声明中,或者使它从NSObject继承。否则它对Objective-C不可见。

In your case, RecognizedClass is recognized because it is a subclass of UIViewController, which is a subclass of NSObject.

在你的例子中,被识别的类因为它是UIViewController的子类,它是NSObject的子类。