inheritanceObjective-C基类的Swift类,不会转换为正确的类型

  • 使用Xcode 7
  • 斯威夫特2

我一直在研究在Swift UIViewController中inheritanceObjective C基类。 我已正确添加了我的Bridging头文件,它正好是我的项目构建设置的一部分:

在此处输入图像描述

在我的SCS-Bridging-Header.h文件中,我有以下代码:

#import "UIViewControllerPlus.h" 

然后我试图在Swift类中inheritanceObjective C类UIViewControllerPlus:

 import UIKit class TestBasicTabBarSegueViewController: UIViewControllerPlus { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

这构建并运行,但是当我查看Swift TestBasicTabBarSegueViewController ,无法访问Objective C基类或超类的属性:

 #ifndef UIViewControllerPlus_h #define UIViewControllerPlus_h #import  #import "MMTabBarController.h" @interface UIViewControllerPlus : UIViewController @property MMTabBarController* mt; @end #endif /* UIViewControllerPlus_h */ 

它是属性mt ,属于MMTabBarController类型。

您可以在以下自定义Segue中看到destinationController.mt无法访问,并在尝试设置时导致应用程序崩溃:

 #import "MMNavigationSegue.h" #import "MMTabBarController.h" #import "UIViewControllerPlus.h" @implementation MMNavigationSegue - (void) perform { MMTabBarController *tabBarController = (MMTabBarController *) self.sourceViewController; UIViewControllerPlus *destinationController = (UIViewControllerPlus *) self.destinationViewController; destinationController.mt = tabBarController; //HERE APP CRASHES for (UIView *view in tabBarController.placeholderView.subviews) { [view removeFromSuperview]; } // Add view to placeholder view tabBarController.currentViewController = destinationController; [tabBarController.placeholderView addSubview: destinationController.view]; // Set autoresizing [tabBarController.placeholderView setTranslatesAutoresizingMaskIntoConstraints:NO]; UIView *childview = destinationController.view; [childview setTranslatesAutoresizingMaskIntoConstraints: NO]; // fill horizontal [tabBarController.placeholderView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[childview]|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(childview)]]; // fill vertical [tabBarController.placeholderView addConstraints:[ NSLayoutConstraint constraintsWithVisualFormat: @"V:|-0-[childview]-0-|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(childview)]]; [tabBarController.placeholderView layoutIfNeeded]; // notify did move [destinationController didMoveToParentViewController: tabBarController]; } @end 

注意,如果目标UIViewController Segued的目标C代码UIViewController实现inheritance自Objective C类UIViewControllerPlus ,则上述代码有效。

但在下一行该应用程序崩溃:

在此处输入图像描述

错误:

 2016-06-14 21:16:39.795 SCS[49747:17004544] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setMt:]: unrecognized selector sent to instance 0x7d967080' 

destinationController没有强制转换为UIViewControllerPlus类型,而是转换为类型UIViewController ,即使它定义为UIViewControllerPlus 。 只有当它是inheritanceObjective C UIViewControllerPlus类的Swift文件/类时才会发生这种情况,但如果它是Objective C子类inheritanceObjective C基类UIViewControllerPlus那么它就会起作用:

在此处输入图像描述

**是否可以在Swift中inheritanceObjective C基类? 我在网上读到了各种相互矛盾的陈述?

我的Objective C项目引用/配置中是否存在我可能缺少的内容?**

-[UIViewController setMt:]:表示正在实例化的视图控制器是常规视图控制器 – 不是您的子类“加”类型之一。

您可能忘记将正确的类分配给故事板中的视图控制器。