CMutablePointer< CString>快速转换为字符串 [英] CMutablePointer<CString> to String in swift-language
本文介绍了CMutablePointer< CString>快速转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个库,其功能如下:
I have a library which has a function like this:
int get_user_name(const char **buffer);
快速调用,应该像这样:
in swift, should call like this:
var name:CMutablePointer<CString> = nil
get_user_name(name)
我想使此功能使用起来更舒适,所以我包装了
I want make use this function more comfortable so I wrapped this up:
func get_username() -> String {
var name:CMutablePointer<CString> = nil
get_user_name(name)
// how to convert name to String
}
我的问题是如何将名称转换为字符串
I question is how to convert name to String
推荐答案
它类似于:
var stringValue :CString = ""
name.withUnsafePointer {p in
stringValue = p.memory
}
return NSString(CString: stringValue)
这篇关于CMutablePointer< CString>快速转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文