我有一个C ++头文件(名为PlusTypeViewController *controller1 = [[PlusTypeViewController alloc]initWithNibName:@"PlusTableView" bundle:nil];
controller1.title = @"PLUSr";
CapsulesViewController *controller2 = [[CapsulesViewController alloc]initWithNibName:@"CapsulesView" bundle:nil];
controller2.title = @"SUBSTITUTEr";
TabletsViewController *controller3 = [[TabletsViewController alloc] initWithNibName:@"Tablets" bundle:nil];
controller3.title = @"SUPPLIERS";
InjectionViewController *controller4 = [[InjectionViewController alloc] initWithNibName:@"Injection" bundle:nil];
controller4.title = @"INJECTION";
ShasheViewController *controller5 = [[ShasheViewController alloc] initWithNibName:@"Shesha" bundle:nil];
controller5.title = @"Shashe";
NSArray *controllerArray2 = @[controller1, controller2,controller3,controller4,controller5];
NSDictionary *parameterss = @{
CAPSPageMenuOptionScrollMenuBackgroundColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
//CAPSPageMenuOptionViewBackgroundColor: [UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
//CAPSPageMenuOptionSelectionIndicatorColor: [UIColor greenColor],
CAPSPageMenuOptionSelectionIndicatorColor:[UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
CAPSPageMenuOptionUnselectedMenuItemLabelColor:[UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
//CAPSPageMenuOptionSelectionIndicatorColor:[UIColor greenColor],
CAPSPageMenuOptionSelectedMenuItemLabelColor :[UIColor blackColor],
//CAPSPageMenuOptionBottomMenuHairlineColor: [UIColor colorWithRed:19.0/255.0 green:112.0/255.0 blue:103.0/255.0 alpha:1.0],
//CAPSPageMenuOptionBottomMenuHairlineColor: [UIColor blackColor],
CAPSPageMenuOptionMenuItemFont: [UIFont fontWithName:@"HelveticaNeue" size:13.0],
CAPSPageMenuOptionMenuHeight: @(30.0),
CAPSPageMenuOptionMenuItemWidth: @(90.0),
//CAPSPageMenuOptionCenterMenuItems: @(YES)
};
_secondPageMenu = [[CAPSPageMenu alloc] initWithViewControllers:controllerArray2 frame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height) options:parameterss];
[self.view addSubview:_secondPageMenu.view];
),我希望将其包含在我的Swift项目中。
由于我想要包含的C ++框架尚未完成,我现在只有头文件。
我的C ++头文件header.h
看起来有点像这样:
header.h
问题是,当我尝试在#include <vector>
struct someStruct{
float someAttr;
}
class someClass{
public:
enum SomeEnum{
Option1,
Option2
}
void someFunc(const double value) {}
}
中包含header.h
文件时,它将永远找不到我在header.h中包含的向量
我尝试将project-Bridging-Header.h
重命名为header.h
。
我尝试在右侧面板中将桥接标题类型设置为C ++标题。
但他们都没有帮助。
我希望你们中的一些人可以帮助我弄清楚我做错了什么。
答案 0 :(得分:2)
不幸的是,直接在Swift中使用C ++类是不可能的,请参阅https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216-CH2-ID0:
您无法直接将C ++代码导入Swift。相反,创建一个 用于C ++代码的Objective-C或C包装器。
实际上,包装C ++以便在Swift中使用的一种简便方法是Objective-C ++。 Objective-C ++源文件可以包含Objective-C和C ++代码,混合使用。以下是基于您问题中的代码段的快速部分示例。此处仅部分包含someClass
。在生产代码中,您还需要考虑内存管理。
包装器的头文件mywrapper.h
没有C ++的痕迹:
#ifndef mywrapper_h
#define mywrapper_h
#import <Foundation/Foundation.h>
// This is a wrapper Objective-C++ class around the C++ class
@interface someClass_oc : NSObject
-(void)someFunc:(double)value;
@end
#endif /* mywrapper_h */
这是Objective-C ++实现,mywrapper.mm
。请注意.mm
扩展程序。您可以使用.m
创建一个Objective-C文件,然后重命名它。
#import "mywrapper.h"
#import "header.h" // CAN import a C++ header here, in Objective-C++ code
// Use an extension on someClass_oc because we need to use someClass,
// but we couldn't do it in mywrapper.h,
// which is visible from Swift and thus can't contain C++ stuff.
@interface someClass_oc ()
{
someClass * ptrSomeClass;
}
@end
@implementation someClass_oc
-(id)init
{
// In this example ptrSomeClass is leaked...
ptrSomeClass = new someClass();
return self;
}
-(void)someFunc:(double)value
{
ptrSomeClass->someFunc(value);
}
@end
现在你可以在桥接头中导入mywrapper.h
,然后在Swift中执行类似的操作:
let x = someClass_oc()
x.someFunc(123.456)
因此,您可以在Swift中创建一个对象,该对象由您的C ++类实例支持。
这只是一个给你一个想法的简单例子。如果你遇到其他问题,他们可能会得到单独的问题。