简体   繁体   English

从Swift生成的Objective-C标头

[英]Generated Objective-C header from Swift

I have problem with generated ObjC header file ( Project-Swift.h ) from Swift classes. 我有从Swift类生成的ObjC头文件( Project-Swift.h )的问题。

Header file contains representation of defined Swift classes and all imports of required dependencies. 头文件包含定义的Swift类的表示形式和所有必需依赖项的导入。 Actually I combine ObjC with Swift in one project together. 实际上,我将ObjC和Swift结合在一个项目中。 Everything works fine until I would like create subclass XYZTableViewController of Swift class ABCTableViewController from external framework. 一切工作正常,直到我想创建子XYZTableViewController斯威夫特类的ABCTableViewController从外部框架。

Definition of class from swift file: swift文件中类的定义:

@objc class XYZTableViewController: ABCTableViewController { ... }

Generated content from Project-Swift.h : Project-Swift.h生成的内容:

SWIFT_CLASS("_TtC6Abc22XYZTableViewController") @interface XYZTableViewController : ABCTableViewController - (nullable instancetype)initWithCoder:(NSCoder * _Nonnull)aDecoder OBJC_DESIGNATED_INITIALIZER; - (nonnull instancetype)initWithNibName:(NSString * _Nullable)nibNameOrNil bundle:(NSBundle * _Nullable)nibBundleOrNil OBJC_DESIGNATED_INITIALIZER; - (nonnull instancetype)initWithStyle:(UITableViewStyle)style OBJC_DESIGNATED_INITIALIZER; @end

Compilation finish with this error: 编译完成,出现以下错误:

Project-Swift.h:140:37: Cannot find interface declaration for 'ABCTableViewController', superclass of 'XYZTableViewController'; Project-Swift.h:140:37:找不到'XYZTableViewController'的超类'ABCTableViewController'的接口声明; did you mean 'UITableViewController'? 您是说'UITableViewController'吗?

It works if I'm importing module into .m files for the external framework and defining properties of related class type. 如果我将模块导入外部框架的.m文件并定义相关类类型的属性,则它可以工作。

Does have anybody that problem or another similar complications? 是否有人有这个问题或其他类似的并发症?

You cannot subclass a Swift class in Objective-C. Taken from Using Swift with Cocoa and Objective-C . 取自将Swift与Cocoa和Objective-C结合使用
So yes, you can use the ABCTableViewController as a @property for composition, but you cannot inherit from it. 因此,可以的,您可以将ABCTableViewController用作合成的ABCTableViewController ,但不能从其继承。

I found problem in project structure. 我在项目结构中发现问题。 I don't know why, but project contains unused ObjC++ source .mm and in this situation compiler ignore project settings and also doesn't generate modules. 我不知道为什么,但是项目包含未使用的ObjC ++源.mm,在这种情况下,编译器会忽略项目设置,也不会生成模块。

helpful and related page: use of @import when modules are disabled 有用且相关的页面: 禁用模块时使用@import

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM