xcode Objective-C - Swift 类在 Objective-C .h 文件中不可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28244717/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:34:43  来源:igfitidea点击:

objective-c - Swift classes are not visible in Objective-C .h file

objective-cxcodecocoa-touchswift

提问by Oleg

I'm trying to use Swift class in my Objective-C file. I included "moduleName-Swift.h"file in my SuperViewController.mfile, but when I try to declare a public method in SuperViewController.hwith Swift-classas method parameter, I obviously get an error: "Expected a type"

我正在尝试在我的 Objective-C 文件中使用 Swift 类。我"moduleName-Swift.h"在我的SuperViewController.m文件中包含了文件,但是当我尝试SuperViewController.h使用Swift-classas 方法参数声明一个公共方法时,我显然得到了一个错误:"Expected a type"

How can I use swift-classin a header file if I can only include projectModule-Swift.hin .mfiles??

swift-class如果我只能包含projectModule-Swift.h.m文件中,如何在头文件中使用?

回答by Eyeball

Make sure to put @objc before the swiftclass name.

确保将 @objc 放在swift类名之前。

@objc myclassname { ... }

Also add

还添加

@class myclassname;

in your obj-c header filewhere you want to access the swift class

在您要访问 swift 类的obj-c 头文件

回答by andrewz

Remember to import the project generated swift header file in your source file, for example #import <MyProjectName-Swift.h>

记得在你的源文件中导入项目生成的swift头文件,例如 #import <MyProjectName-Swift.h>

回答by Sloba

I had the same problem. Adding @class MyClassName in obj-c .h file created the "Receiver 'MyClassName' for class message is a forward declaration" error, so I deleted that and ONLY added the "@objc" before the swift class name, BUT I also had to make sure that the swift class is a subclass of NSObject.

我有同样的问题。在 obj-c .h 文件中添加 @class MyClassName 创建了“类消息的接收器‘MyClassName’是前向声明”错误,所以我删除了它,只在 swift 类名之前添加了“@objc”,但我也有确保 swift 类是 NSObject 的子类。

@objc class MySwiftClass:NSObject { ... }

WITHOUT @class MyClassName in obj-c .h file!

obj-c .h 文件中没有@class MyClassName!

回答by Matt H

I had to use:

我不得不使用:

@obj public class MySwiftClass: NSObject { ... }

@obj public class MySwiftClass: NSObject { ... }

回答by Ronaldo Albertini

I created a class with no subclass, adding @objc and @objcmembers did not work. After I change do :NSObjectthe entire class turn visible to objective-C

我创建了一个没有子类的类,添加 @objc 和 @objcmembers 不起作用。在我更改后:NSObject,整个班级都对 Objective-C 可见