3

I am trying to migrate a UIViewController Objective-C class to Swift. This view controller is inheriting from a BaseViewController where I have common functionality that I want to have in all controllers. The problem I am having is that the generated myproject-Swift.h is not able to find my BaseViewController.

Is there any way to implement a UIViewController in swift that inherits from a BaseViewController (subclass of UIViewController) written in Objective-C? Is there a bridging problem?

It can be reproduced with this minimal code:

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;
}

projectname-Bridging-Header.h

#import "BaseViewController.h"
atxe
  • 5,029
  • 2
  • 36
  • 50
  • What does your objc bridging header look like? – Nate Cook Jun 20 '14 at 13:19
  • @NateCook Updated. It's pretty simple. – atxe Jun 20 '14 at 13:38
  • Just having a bridging header isn't enoough, is it set up properly? See http://stackoverflow.com/questions/24272184/connect-objective-c-framework-to-swift-ios-8-app-parse-framework/24272431#24272431 – Jack Jun 20 '14 at 14:15
  • @JackWu I think so. The header is included in "Objective-C Bridging Header". – atxe Jun 20 '14 at 14:52
  • 1
    Interesting. Looks like you have all the required parts in the right format & location. ...Maybe a dumb question, but did you actually build/run the project? That's required for the generated .swift header. Somewhere in the WWDC "Integrating With Objective-C" video there was also mention of using @class forward declaration, but I don't recall specifics off the top of my head... – mc01 Jun 20 '14 at 16:57
  • I built a single-view project with all these settings and it worked fine - I could even declare a property in "BaseViewController.h" and access it in the subclass. Maybe a bug? Can you try recreating the project step by step? – Nate Cook Jun 20 '14 at 17:34
  • @mc01 Yes, it's built. And yes, it seems to be related with the order on how each source file is built. – atxe Jun 23 '14 at 07:28
  • @NateCook I repeated the steps and I had the same error :-( – atxe Jun 23 '14 at 07:29

2 Answers2

5

As pointed out in the accepted answer on How can I add forward class references used in the -Swift.h header?

Interoperability guide (Importing Swift into Objective-C):

If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.

The example is solved by importing BaseViewController before the importing projectname-Swift.h in:

AppDelegate.m

#import "AppDelegate.h"
#import "BaseViewController.h"
#import "projectname-Swift.h"   // Replace with your project name
// ...
Community
  • 1
  • 1
atxe
  • 5,029
  • 2
  • 36
  • 50
0

It looks like they have fixed the issue. Currently under XCode6-Beta6 the problem reported by @atxe does not occur anymore. Therefor you can finally roll your AppDelegate.m header back to:

#import "AppDelegate.h"
#import "projectname-Swift.h"   // Replace with your project name
Mr Spiegel
  • 66
  • 5