Swift does not import typedef from Objective-C Header
Asked Answered
W

2

12

It seems that Swift does not recognize a typedef in a Objective-C-Header as I get following error:

Could not find a user-defined conversion from type 'MMDrawerControllerDrawerVisualStateBlock!' to type '(MMDrawerController!, MMDrawerSide, CGFloat) -> Void'

I use the MMDrawerController which is written in Objective-C, my own code though is in Swift.

The typedef looks like this:

typedef void (^MMDrawerControllerDrawerVisualStateBlock)(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible);

Here are more code snippets for clarity:

AppDelegate.swift

func initDrawerController() {
    drawerController = MMDrawerController(centerViewController: centerController, leftDrawerViewController: leftDrawerController, rightDrawerViewController: rightDrawerController)
    drawerController?.setDrawerVisualStateBlock(MMDrawerVisualState.parallaxVisualStateBlockWithParallaxFactor(2.0))
}

MMDrawerController.h

typedef void (^MMDrawerControllerDrawerVisualStateBlock)(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible);

@interface MMDrawerController : UIViewController
-(void)setDrawerVisualStateBlock:(void(^)(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible))drawerVisualStateBlock;
@end

MMDrawerVisualState.h

@interface MMDrawerVisualState : NSObject
+(MMDrawerControllerDrawerVisualStateBlock)parallaxVisualStateBlockWithParallaxFactor:(CGFloat)parallaxFactor;
@end

Module-Bridging-Header.h

#import "MMDrawerController.h"
#import "MMDrawerVisualState.h"

When building this, I get an error in my AppDelegate for the Expression with setDrawerVisualStateBlock, although there is a typedef in the MMDrawerController.h:

Is this a bug (because on Objective-C, it works fine)? Or is there anyone who knows/has an idea how to deal with it? Help is much appreciated, thanks!

Wicket answered 16/8, 2014 at 8:46 Comment(0)
F
3

Objective-C typedef declaration (in MMDrawerController.h for example) :

typedef NS_ENUM(NSInteger, MMDrawerOpenCenterInteractionMode) {
    MMDrawerOpenCenterInteractionModeNone,
    MMDrawerOpenCenterInteractionModeFull,
    MMDrawerOpenCenterInteractionModeNavigationBarOnly,
};

In Swift, it’s imported like this :

enum MMDrawerOpenCenterInteractionMode: NSInteger {
    case None
    case Full
    case Only
}

So you can use this syntax in Swift :

var mmDrawerViewController:MMDrawerController! = ....... ;
mmDrawerViewController.centerHiddenInteractionMode = MMDrawerOpenCenterInteractionMode.Full;

For use mmDrawerViewController.setDrawerVisualStateBlock() try

mmDrawerViewController.setDrawerVisualStateBlock{ (drawerController:MMDrawerController!, drawerSlide:MMDrawerSide!, cgFloat:CGFloat!) -> Void in
    println("setDrawerVisualStateBlock");
    ...
}
Febrifugal answered 11/9, 2014 at 14:28 Comment(2)
Yeah but you cant use the default blocks like MMDrawerVisualState.swingingDoorVisualStateBlock()Asphyxiant
Change NSUInteger to NSInteger helps me.Wong
B
1

The reason is that you access MMDrawerSide drawerSide but are probably not importing the file that declares MMDrawerSide in your bridging header.

If an Objective-C method declaration accesses a class which is not imported in your bridging header, Swift doesn't import nor recognize that method.

So, if you have a typedef in Objective-C like this:

typedef void (^someBlock)(MMSomeClass *someInstance);

Then you must make sure that MMSomeClass is imported in your bridging header.

Bloomsbury answered 7/10, 2015 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.