从Swift的Objective-c基本视图控制器继承

atx*_*txe 3 objective-c uiviewcontroller ios swift

我正在尝试将UIViewControllerObjective-C类迁移到Swift.此视图控制器继承自我BaseViewController在所有控制器中具有的常用功能.我遇到的问题是生成myproject-Swift.h的无法找到我的BaseViewController.

有没有办法实现一个UIViewController从Objective-C中编写的BaseViewController(子类UIViewController)继承的swift ?有桥接问题吗?

它可以用这个最小的代码重现:

BaseViewController.h

#import <UIKit/UIKit.h>

@interface BaseViewController : UIViewController 
@end
Run Code Online (Sandbox Code Playgroud)

BaseViewController.m

import "BaseViewController.h"

@implementation BaseViewController
@end
Run Code Online (Sandbox Code Playgroud)

ViewController.swift

import UIKit

class ViewController : BaseViewController {

}
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

#import "AppDelegate.h"
#import "projectname-Swift.h"   // Replace with your project name

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

项目名称桥接,Header.h

#import "BaseViewController.h"
Run Code Online (Sandbox Code Playgroud)

atx*_*txe 5

正如在接受的答案中所指出的,如何添加-Swift.h头中使用的前向类引用?

互操作性指南(将Swift导入Objective-C):

如果在Swift代码中使用自己的Objective-C类型,请确保在将Swift生成的头导入要从中访问Swift代码的Objective-C .m文件之前导入这些类型的Objective-C头.

该示例通过在导入BaseViewController之前导入来解决projectname-Swift.h:

AppDelegate.m

#import "AppDelegate.h"
#import "BaseViewController.h"
#import "projectname-Swift.h"   // Replace with your project name
// ...
Run Code Online (Sandbox Code Playgroud)