本文介绍了斯威夫特转换CCHAR数组转换成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些C的API,例如glGetShaderInfoLog,在缓冲区返回字符数组。我需要将它们转换为字符串使用它们。

Some C APIs, e.g. glGetShaderInfoLog, return character arrays in buffers. I need to convert them to Strings to use them.

var value: GLint = 0
glGetShaderiv(shader, GLenum(GL_INFO_LOG_LENGTH), &value)
var infoLog: GLchar[] = GLchar[](count: Int(value), repeatedValue: 0)
var infoLogLength: GLsizei = 0
glGetShaderInfoLog(shader, value, &infoLogLength, &infoLog)
var s: String = NSString.stringWithUTF8String(infoLog) // Compile Error: Cannot convert the expression's type 'NSString!' to type 'CString'

在这个例子中GLchar映射到雨燕CCHAR AKA INT8但对我的生活,我不能找到一个字符串或NSString的方法,将它初始化。

In this example GLchar maps to the Swift type CChar AKA Int8 but for the life of me I can't find a String or NSString method that will initialize with it.

推荐答案

这是卓有成效的:

var s = NSString(bytes: infoLog, length: Int(infoLogLength), encoding: NSASCIIStringEncoding)

这篇关于斯威夫特转换CCHAR数组转换成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:10