在Swift中包含C ++头文件 [英] Include C++ header file in Swift

查看:131
本文介绍了在Swift中包含C ++头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++头文件(名为 header.h ),我希望将其包含在我的Swift项目中。

I have a C++ header file (named header.h) which I want to include into my Swift project.

由于我想要包含的C ++框架尚未完成,我现在只有头文件。

Since the C++ framework I want to include is not finished yet I just have the header file for now.

我的C ++头文件 header .h 看起来有点像这样:

My C++ header file header.h looks a little like this:

#include <vector>

struct someStruct{
    float someAttr;
}

class someClass{
    public:
        enum SomeEnum{
            Option1,
            Option2
        }

        void someFunc(const double value) {}
}

问题是,何时我尝试在项目-Bridging-Header.h 中包含 header.h 文件,它永远不会找到vector我在header.h中包含

Problem is, when I try to include the header.h file in the project-Bridging-Header.h it will never find vector which I include in header.h

< img src =https://i.stack.imgur.com/VQfZW.pngalt ='找不到'矢量'文件>

我尝试将 header.h 重命名为 header.hpp
我尝试在右侧面板中设置桥接标题类型为C ++标题。
但他们都没有帮助。

I tried renaming header.h to header.hpp. I tried setting the bridging headers Type to C++ Header in the right panel. But none of them helped.

我希望你们中的一些人可以帮我弄清楚我做错了什么。

I hope some of you can help me figure out what I am doing wrong.

推荐答案

不幸的是,直接在Swift中使用C ++类是不可能的,参见 https://developer.apple.com/library/ios/documentation/Swift /Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216-CH2-ID0

Unfortunately, it is not possible to use a C++ class in Swift directly, see 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包装器。

You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.

实际上,一种方便的包装C ++的方法在Swift中使用Objective-C ++。 Objective-C ++源文件可以包含Objective-C和C ++代码,混合使用。以下是基于您问题中的代码段的快速部分示例。只有 someClass 部分包含在这里。在生产代码中,您还需要考虑内存管理。

Actually, a convenient way of wrapping C++ for use in Swift is Objective-C++. Objective-C++ source files can contain both Objective-C and C++ code, mixed. Here is a quick partial example based on the code snippet in your question. Only someClass is partially wrapped here. In production code you would also need to consider memory management.

包装器的头文件 mywrapper.h ,没有C ++的痕迹:

The wrapper's header file, mywrapper.h, has no traces of 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文件,然后重命名它。

Here is the Objective-C++ implementation, mywrapper.mm. Please note the .mm extension. You can create an Objective-C file with an .m and then rename it.

    #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

现在你可以导入<$在桥接标题中c $ c> mywrapper.h 然后在Swift中执行类似的操作:

Now you can import mywrapper.h in the bridging header and then do something like this in Swift:

let x = someClass_oc()

x.someFunc(123.456)

因此,您可以在Swift中创建一个对象,该对象由C ++类的实例支持。

Thus you can create an object in Swift, which is backed by an instance of your C++ class.

这只是一个简单的例子,可以给你一个想法。如果你遇到其他问题,他们可能会得到单独的问题。

This is just a quick example to give you an idea. If your run into other problems, they would probably deserve separate questions.

这篇关于在Swift中包含C ++头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆