【发布时间】:2014-06-20 13:17:53
【问题描述】:
我正在尝试将 UIViewController Objective-C 类迁移到 Swift。此视图控制器继承自 BaseViewController,在该 BaseViewController 中,我拥有我希望在所有控制器中拥有的通用功能。我遇到的问题是生成的myproject-Swift.h 无法找到我的BaseViewController。
有什么方法可以在 swift 中实现一个 UIViewController,它继承自用 Objective-C 编写的 BaseViewController(UIViewController 的子类)?是否存在桥接问题?
可以用这个最少的代码复制它:
BaseViewController.h
#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController
@end
BaseViewController.m
import "BaseViewController.h"
@implementation BaseViewController
@end
ViewController.swift
import UIKit
class ViewController : BaseViewController {
}
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;
}
项目名称-Bridging-Header.h
#import "BaseViewController.h"
【问题讨论】:
-
你的 objc 桥接头是什么样的?
-
@NateCook 已更新。这很简单。
-
仅仅有一个桥接头是不够的,它设置正确吗?见stackoverflow.com/questions/24272184/…
-
@JackWu 我想是的。标头包含在“Objective-C Bridging Header”中。
-
有趣。看起来您拥有正确格式和位置的所有必需部件。 ...也许是一个愚蠢的问题,但您是否真的构建/运行了该项目?这是生成的 .swift 标头所必需的。在 WWDC“与 Objective-C 集成”视频中的某处也提到了使用 @class 前向声明,但我不记得具体细节...
标签: ios objective-c uiviewcontroller swift