ios 将 swift 框架导入到 Objective-C 项目中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31099596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
importing swift framework into a objective-c project
提问by Michael A
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
我还在头文件 MyFramework-Swift.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 项目时,同样的程序也能工作
回答by saltwat5r
If you previously configured Project for integrating with Swift and want to use Swift Dynamic Framework, you have to import it like this way (replace {value} with appropriate names depending on your Project):
如果您之前将 Project 配置为与 Swift 集成并希望使用 Swift 动态框架,则必须以这种方式导入它(根据您的项目将 {value} 替换为适当的名称):
#import <{MyFramework}/{MyFrameworkMainClass}-Swift.h>
#import "{YourProjectTargetName}-Swift.h"
EDIT:
编辑:
If your framework has Defines Module
set to true
, then you can import it like this:
如果您的框架已Defines Module
设置为true
,那么您可以像这样导入它:
@import MyFramework;
回答by fat
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 中访问和使用。这个属性告诉编译器这段 Swift 代码可以从 Objective-C 访问。如果您的 Swift 类是 Objective-C 类的后代,编译器会自动为您添加 @objc 属性。
回答by Cihan Tek
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
.
在您的情况下,RecognizedClass
被识别是因为它是 的子类UIViewController
,它是 的子类NSObject
。
回答by yoAlex5
Using Swift Classes in Objective-C
在 Objective-C 中使用 Swift 类
If you are going to import code within an App Target (Mixing Objective-C and Swift in one project) you should use the next import line #import "<#YourProjectName#>-Swift.h"
to expose Swift code to Objective-C code [Mixing Swift and Objective-C code in a project]
如果您要在 App Target 中导入代码(在一个项目中混合 Objective-C 和 Swift),您应该使用下一个导入行将#import "<#YourProjectName#>-Swift.h"
Swift 代码公开给 Objective-C 代码[在项目中混合 Swift 和 Objective-C 代码]
In this post I will describe how to import Swift framework to Objective-C code
在这篇文章中,我将描述如何将 Swift 框架导入到 Objective-C 代码中
Objective-C consumer -> Swift dynamic framework
Objective-C 消费者 -> Swift 动态框架
Xcode version 10.2.1
Xcode 版本 10.2.1
Create a Swift framework
创建一个 Swift 框架
Follow Create Swift framework
Expose Swift API. To use Swift's functions from Objective-C[About]
公开 Swift API。在 Objective-C 中使用 Swift 的函数[关于]
Objective-C consumer with Swift static library
带有 Swift 静态库的 Objective-C 使用者
Follow Using Swift framework
Import module to the Objective-C client code[module_name]
将模块导入 Objective-C 客户端代码[module_name]
@import module_name;