[英]I've tried it all, but I can't import my Swift file to my Objective-C file
I've read though multiple threads on this on StackOverflow, and I can't seem to find the answer to my problem. 我已经在StackOverflow上阅读了多个线程,但似乎找不到我的问题的答案。 My folder hierarchy is like this: 我的文件夹层次结构是这样的:
"Project Name" - "Helpers" "FileIWantToImport.swift" "FileIWantToImportTo.m"
I have: 我有:
FMB
设置构建设置以使产品模块名称为FMB
Defines Modules
to YES
将“ Defines Modules
YES
设置为YES
#import FMB-swift.h
in my AppDelegate with out any problems #import FMB-swift.h
在我的AppDelegate中#import FMB-swift.h
,没有任何问题 @objc
to my class declaration in my file so it says @objc class MyClass: NSObject
在我的文件的类声明中添加了@objc
,因此它表示了@objc class MyClass: NSObject
HOWEVER. 然而。 I cannot, for the life me, figure out why it won't import into FileIWantToImportTo.m
我一辈子都无法弄清楚为什么它不会导入FileIWantToImportTo.m
I have tried: 我努力了:
#import "FMB-swift.h"
#import "Helpers/FMB-swift.h"
#import "Product Name/Helpers/FMB-swift.h"
#import <FMB-swift.h>
#import <Helpers/FMB-swift.h>
#import <Product Name/Helpers/FMB-swift.h>
All I get is "file not found"
error. 我得到的只是"file not found"
错误。 Can someone please point me in the right direction? 有人可以指出正确的方向吗? I've looked through all the other threads and the apple docs and can't figure it out. 我已经浏览了所有其他线程和苹果文档,但无法弄清楚。 I'm running in Xcode 7 and 10.11 GMs if that makes a difference. 如果这有所作为,我将在Xcode 7和10.11 GM中运行。
EDIT: Here is an example project https://github.com/Aghassi/Example-Project 编辑:这是一个示例项目https://github.com/Aghassi/Example-Project
This is what you need to do: 这是您需要做的:
Create a Swift file in your ObjC based project. 在基于ObjC的项目中创建一个Swift文件。 No need to create bridging headers, since they're used to see ObjC code from swift, not swift code from ObjC. 无需创建桥接头,因为它们用于从swift中查看ObjC代码,而不是从ObjC中查看swift代码。
In your Target, under Build Settings set Embedded Content Contains Swift Code
to Yes 在目标中的“构建设置”下,将“ Embedded Content Contains Swift Code
”设置为“是”
Your Swift file should have an @objc public class. 您的Swift文件应具有@objc公共类。
@objc public class Example: NSObject { public func printSomething(text: String) { print(text); } }
Check your Project's name, and in your .m file, add #import <ProjectsName-Swift.h>
, and you should be able to instantiate your class defined in the Swift file, and all it's public methods. 检查项目的名称,并在.m文件中添加#import <ProjectsName-Swift.h>
,您应该能够实例化Swift文件及其所有公共方法中定义的类。 You might need to build your project before importing the -Swift.h file. 在导入-Swift.h文件之前,您可能需要先构建项目。
Here's an example project with an ObjC class using Swift code: https://github.com/lucaslt89/Example-Project.git 这是一个使用Swift代码的ObjC类的示例项目: https : //github.com/lucaslt89/Example-Project.git
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.