Swift不从Objective-C Header导入typedef

时间:2014-08-16 08:46:34

标签: ios objective-c swift typedef mmdrawercontroller

似乎Swift无法识别Objective-C-Header中的typedef,因为我收到以下错误:

无法从类型' MMDrawerControllerDrawerVisualStateBlock中找到用户定义的转化!'键入'(MMDrawerController!,MMDrawerSide,CGFloat) - >空隙'

我使用的是用Objective-C编写的MMDrawerController,我自己的代码虽然在Swift中。

typedef如下所示:

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

为清晰起见,以下是更多代码段:

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

模块-桥接-Header.h

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

构建此内容时,我的AppDelegate中的setDrawerVisualStateBlock表达式出现错误,尽管MMDrawerController.h中有一个typedef:

这是一个错误(因为在Objective-C上,它工作正常)?或者有谁知道/有想法如何处理它? 非常感谢帮助,谢谢!

2 个答案:

答案 0 :(得分:3)

Objective-C typedef声明(例如在MMDrawerController.h中):

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

在Swift中,它是这样导入的:

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

所以你可以在Swift中使用这个语法:

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

使用mmDrawerViewController.setDrawerVisualStateBlock() 尝试

mmDrawerViewController.setDrawerVisualStateBlock{ (drawerController:MMDrawerController!, drawerSlide:MMDrawerSide!, cgFloat:CGFloat!) -> Void in
    println("setDrawerVisualStateBlock");
    ...
}

答案 1 :(得分:1)

原因是您访问MMDrawerSide drawerSide但可能没有导入在桥接标头中声明MMDrawerSide的文件。

如果Objective-C方法声明访问未在您的桥接头中导入的类,则Swift不会导入也不会识别该方法。

所以,如果你在Objective-C中有这样的typedef:

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

然后,您必须确保在桥接标头中导入MMSomeClass