如何将 Swift 代码导入到 Objective-C? [英] How can I import Swift code to Objective-C?
问题描述
我用 Swift 编写了一个库,但无法将它导入到我当前用 Objective-C 编写的项目中.
I have written a library in Swift and I wasn't able to import it to my current project, written in Objective-C.
有什么办法可以导入吗?
Are there any ways to import it?
#import "SCLAlertView.swift" - 'SCLAlertView.swift' file not found
推荐答案
您需要导入 ProductName-Swift.h
.请注意,这是产品名称 - 其他答案错误地使用了类名称.
You need to import ProductName-Swift.h
. Note that it's the product name - the other answers make the mistake of using the class name.
这个单个文件是一个自动生成的头文件,它为您项目中的所有 Swift 类定义了 Objective-C 接口,这些类要么用 @objc
注释,要么从 NSObject
继承.
This single file is an autogenerated header that defines Objective-C interfaces for all Swift classes in your project that are either annotated with @objc
or inherit from NSObject
.
注意事项:
如果您的产品名称包含空格,请将其替换为下划线(例如,
My Project
变为My_Project-Swift.h
)
如果你的目标是框架,则需要导入
If your target is a framework, you need to import <ProductName/ProductName-Swift.h>
确保您的 Swift 文件是目标的成员
Make sure your Swift file is member of the target
这篇关于如何将 Swift 代码导入到 Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!