如何从Swift传递两个字符串到c++函数,并获得一个整数数组?

时间:2021-10-11 12:47:32

I am trying to pass two Strings from Swift to the C++ function, handle them there, and then return an array of integers (probally a vector), not sure yet.

我尝试将两个字符串从Swift传递到c++函数,在那里处理它们,然后返回一个整数数组(可能是一个矢量),还不确定。

I've tried it with (C++ function declaration):

我试过(c++函数声明):

void exmaple(char* one, char* two);

But was not able to pass the strings from Swift to C++.

但是无法将字符串从Swift传递到c++。

Then I changed the function a little:

然后我稍微改变了一下函数:

void example(void* one, void* two)
{
    string &a = *reinterpret_cast<string*>(one);
    string &b = *reinterpret_cast<string*>(two);

    cout<<"String b is: "<<b<<endl;
}

and tried calling the function in Swift:

试着用Swift调用这个函数:

let aStr = UnsafeMutablePointer<String>.alloc(10)
aStr.initialize("stringA")

let bStr = UnsafeMutablePointer<String>.alloc(10)
bStr.initialize("stringB")

example(aStr, bStr)

But the only output I get is:

但我得到的唯一输出是:

String b is: 

How can I pass two Strings from Swift to a C++ function, and then return an array/vector of integers?

如何将两个字符串从Swift传递到c++函数,然后返回一个整数数组/向量?

1 个解决方案

#1


2  

Swift has some magic bridging that allows you to pass regular Swift strings directly into C functions. For example, strlen has the signature size_t strlen(const char *), which comes into Swift as func strlen(_: UnsafePointer<Int8>) -> UInt but you can call it with ordinary strings like this:

Swift有一些神奇的桥接,使您可以将常规的Swift字符串直接传递到C函数中。例如,strlen有签名size_t strlen(const char *),它以func strlen(_: UnsafePointer ) -> UInt形式出现在Swift中,但您可以使用如下普通字符串:

import Darwin

let greeting: String = "Hello!"

strlen(greeting)   // returns 6

strlen("Goodbye")  // returns 7

And the compiler will automatically generate null-terminated C strings for the call.

编译器将自动为调用生成以null结尾的C字符串。

The last part of your question is much trickier unfortunately. Not only is there actually no bridging to C++, only C and Objective-C, there most definitely won't be the ability to pass back a C++ vector. Your best bet is either to go via an Objective-C layer and pass back an NSArray, or to use C-style pass in an empty buffer and have the function write the values into it.

不幸的是,你问题的最后一部分要棘手得多。不仅没有连接到c++,只有C和Objective-C,最明显的是不能传递一个c++向量。您最好的方法是通过Objective-C层并传递NSArray,或者在空缓冲区中使用c样式传递,并让函数将值写入其中。

#1


2  

Swift has some magic bridging that allows you to pass regular Swift strings directly into C functions. For example, strlen has the signature size_t strlen(const char *), which comes into Swift as func strlen(_: UnsafePointer<Int8>) -> UInt but you can call it with ordinary strings like this:

Swift有一些神奇的桥接,使您可以将常规的Swift字符串直接传递到C函数中。例如,strlen有签名size_t strlen(const char *),它以func strlen(_: UnsafePointer ) -> UInt形式出现在Swift中,但您可以使用如下普通字符串:

import Darwin

let greeting: String = "Hello!"

strlen(greeting)   // returns 6

strlen("Goodbye")  // returns 7

And the compiler will automatically generate null-terminated C strings for the call.

编译器将自动为调用生成以null结尾的C字符串。

The last part of your question is much trickier unfortunately. Not only is there actually no bridging to C++, only C and Objective-C, there most definitely won't be the ability to pass back a C++ vector. Your best bet is either to go via an Objective-C layer and pass back an NSArray, or to use C-style pass in an empty buffer and have the function write the values into it.

不幸的是,你问题的最后一部分要棘手得多。不仅没有连接到c++,只有C和Objective-C,最明显的是不能传递一个c++向量。您最好的方法是通过Objective-C层并传递NSArray,或者在空缓冲区中使用c样式传递,并让函数将值写入其中。