如何从纯C函数返回值到swift? [英] How to get return values from pure C function to swift?

查看:130
本文介绍了如何从纯C函数返回值到swift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑,如何从纯粹的 c 函数返回值为swift。



我的代码:

ViewController.swift // swift文件中

  var name = getPersonName()

code> personList.h // C头文件

  char getPersonName(); 

personList.c //纯C file

  #include personList.h 
$ b $ char getPersonName(){
char * name =你好,斯威夫特;
返回名称;
}

这里我已经通过桥连接personList.h文件,使用 MyProjectName-Bridging-Header.h



谢谢

解决方案如果你想C函数返回一个字符串,那么返回类型应该是
char * 或更好 const char *

  // personList.h:
const char * getPersonName(无效);

// personList.c:
const char * getPersonName(void)
{
char * name =Hello,Swift;
返回名称;
}

这是作为 $ b导入Swift $ b

  func getPersonName() - > UnsafePointer< INT8> 

,您可以使用返回的指针创建一个Swift字符串,其中包含

  let name = String.fromCString(getPersonName())! 
println(name)//输出:Hello,Swift

// Swift 3:
let name = String(cString:getPersonName())
print(name )//输出:Hello,Swift

Hooray,你会说,这就是我需要。 - 但等待!!
这只适用于C函数中的Hello,Swift是一个字符串文字的。通常你不能从
函数返回一个指向局部变量的指针,因为指针指向的内存在函数返回后可能不是
有效。如果指针没有将
指向静态内存,那么你必须重复它。示例:

  const char * getPersonName(void)
{
char name [200];
snprintf(name,sizeof name,%s%s,Hello,Swift!);
return strdup(name);
}

但现在调用者必须释放内存内存最终:

  let cstr = getPersonName()
let name = String.fromCString(cstr)!
free(UnsafeMutablePointer(cstr))

println(name)

或者,您可以更改C函数,以便调用者
传递内存:

  void getPersonName char * name,size_t nameSize)
{
snprintf(name,nameSize,%s%s,Hello,Swift!);
}

将从Swift中使用它作为

  var nameBuf = [Int8](count:200,repeatedValue:0)//缓冲区为C字符串
getPersonName(& nameBuf,UInt(nameBuf。 count))
let name = String.fromCString(nameBuf)!
println(名字)

// Swift 3:
var nameBuf = [Int8](重复:0,count:200)// C字符串缓冲区
getPersonName(& nameBuf,nameBuf.count)
let name = String(cString:nameBuf)
print(name)


I have a doubt, how to get the returned value from pure c function to swift.

Here my code:

In ViewController.swift // swift file

var name = getPersonName()

In personList.h // C header file

char getPersonName();

In personList.c// pure C file

#include personList.h

char getPersonName() {
    char* name = "Hello, Swift";
    return name;
}

Here already i linked personList.h file through bridge using MyProjectName-Bridging-Header.h.

Thanks

解决方案

If you want the C function to return a string then the return type should be char * or better const char *:

// personList.h:
const char *getPersonName(void);

// personList.c:
const char *getPersonName(void)
{
    char *name = "Hello, Swift";
    return name;
}

This is imported to Swift as

func getPersonName() -> UnsafePointer<Int8>

and you can create a Swift string from the returned pointer with

let name = String.fromCString(getPersonName())!
println(name) // Output: Hello, Swift

// Swift 3:
let name = String(cString: getPersonName())
print(name) // Output: Hello, Swift

"Hooray," you'll say, "that's what I need." – But wait!! This works only because "Hello, Swift" in the C function is a string literal. Normally you cannot return a pointer to a local variable from a function, because the memory that the pointer points to may not be valid after return from the function. If the pointer does not point to static memory then you have to duplicate it. Example:

const char *getPersonName(void)
{
    char name[200];
    snprintf(name, sizeof name, "%s %s", "Hello", "Swift!");
    return strdup(name);
}

but now the caller has to deallocate the memory eventually:

let cstr = getPersonName()
let name = String.fromCString(cstr)!
free(UnsafeMutablePointer(cstr))

println(name)

Alternatively, you can change the C function so that the caller passes the memory instead:

void getPersonName(char *name, size_t nameSize)
{
    snprintf(name, nameSize, "%s %s", "Hello", "Swift!");
}

which would be used from Swift as

var nameBuf = [Int8](count: 200, repeatedValue: 0) // Buffer for C string
getPersonName(&nameBuf, UInt(nameBuf.count))
let name = String.fromCString(nameBuf)!
println(name)

// Swift 3:
var nameBuf = [Int8](repeating: 0, count: 200) // Buffer for C string
getPersonName(&nameBuf, nameBuf.count)
let name = String(cString: nameBuf)
print(name)

这篇关于如何从纯C函数返回值到swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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