简体   繁体   English

我已经尝试了全部,但是无法将Swift文件导入到Objective-C文件中

[英]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: 我有:

  • Set the build settings to have a Product Module Name FMB 设置构建设置以使产品模块名称为FMB
  • Set Defines Modules to YES 将“ Defines Modules YES设置为YES
  • a bridging header created by Xcode Xcode创建的桥接头
  • cleaned my build 清理我的建筑
  • #import FMB-swift.h in my AppDelegate with out any problems #import FMB-swift.h在我的AppDelegate中#import FMB-swift.h ,没有任何问题
  • Added @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: 这是您需要做的:

  1. 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代码。

  2. In your Target, under Build Settings set Embedded Content Contains Swift Code to Yes 在目标中的“构建设置”下,将“ Embedded Content Contains Swift Code ”设置为“是”

在此处输入图片说明

  1. Your Swift file should have an @objc public class. 您的Swift文件应具有@objc公共类。

     @objc public class Example: NSObject { public func printSomething(text: String) { print(text); } } 
  2. 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.

相关问题 为什么我不能在我的 objective-c 代码中使用我的 swift 文件? - Why can't I use my swift files in my objective-c code? 在Objective-C中,是否需要导入每个h文件才能查看对象层次结构? - In Objective-C, do I need to import every h file to see my object hierarchy? 我需要在objective-c 中使用swift 类并且无法设置我的Emilos-Swift.h 桥接文件来解析objective-c 中的任何swift 类 - I need to use a swift class in objective-c and cannot setup my Emilos-Swift.h bridging file to resolve any swift classes in objective-c 无法导入导入Project-Swift.h的Swift桥接头Objective-C文件 - Can't import into Swift bridging header Objective-C file that imports Project-Swift.h 将Swift代码导入Objective-C文件 - Import Swift code to Objective-C file 无法将My Swift类访问到Objective-C应用程序 - Can't access My Swift classes into Objective-C application 我应该在混合的Objective-C / Swift项目中使用Realm Objective-C或Realm Swift吗? - Should I use Realm Objective-C or Realm Swift in my mixed Objective-C / Swift project? 将Objective-C桥接到Swift-看不到文件 - Bridge Objective-C to Swift - can't see file 我可以在Swift框架库中使用Objective-C类吗? - Can I use an Objective-C class in my Swift framework library? 如何从目标 c 中的 pod 文件访问我的 swift 项目文件? - how can I access my swift project files from pod file which is in objective c?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM