[英]Access swift class in objective c parent class
I have created a hybrid App which has both Objective-c and Swift classes.我创建了一个混合应用程序,它同时具有 Objective-c 和 Swift 类。
I have a Main View Controller (parent) from which all other View Controllers are inherited (both written in Objective-c and Swift).我有一个主视图 Controller(父视图),所有其他视图控制器都从该视图控制器继承(均用 Objective-c 和 Swift 编写)。
Now the issue I am facing is that I want to access a swift class inside my MainViewController but when I add #import "MyProjectName-Swift.h"
in MainViewController.h
, I got tons of errors.现在我面临的问题是我想在我的 MainViewController 中访问 swift class 但是当我在
MainViewController.h
添加#import "MyProjectName-Swift.h"
时,我遇到了很多错误。
The reason for that is because my swift view controllers are also inherited from MainViewController
and when I add #import "MyProjectName-Swift.h"
to MainViewController.h
, all swift classes got confused.原因是我的 swift 视图控制器也是从
MainViewController
继承的,当我将#import "MyProjectName-Swift.h"
添加到MainViewController.h
时,所有 swift 类都被混淆了。
I hope you understand my question.我希望你能理解我的问题。 The scenario is a bit complicated and sorry if I haven't explained it any better.
如果我没有更好地解释它,这个场景有点复杂和抱歉。
Also, is there a way to import only a single swift class in objective-c instead of doing this #import "MyProjectName-Swift.h"
?另外,有没有办法在 objective-c 中只导入一个 swift class 而不是这样做
#import "MyProjectName-Swift.h"
?
Any kind of help would be greatly appreciated.任何形式的帮助将不胜感激。
You can only import "ProductName-Swift.h" in.m files.您只能在.m 文件中导入“ProductName-Swift.h”。
To avoid cyclical references, don't import Swift into an Objective-C header file.
为避免循环引用,请勿将 Swift 导入 Objective-C header 文件。 Instead, you can forward declare a Swift class to use it in an Objective-C header.
相反,您可以转发声明 Swift class 以在 Objective-C Z099FB995346F31C7549F6E40DB0E3Z 中使用它。 Note that you cannot subclass a Swift class in Objective-C.
请注意,您不能在 Objective-C 中子类化 Swift class。
There is no way to import single Swift class to ObjC file.无法将单个 Swift class 导入到 ObjC 文件。
ProductName-Swift.h
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
. ProductName-Swift.h
是一个自动生成的 header,它为项目中的所有 Swift 类定义了 Objective-C 接口,这些接口要么使用@objc
注释,要么继承自NSObject
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.