本文介绍了iOS:转换UnsafeMutablePointer< Int8>在swift中串起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,在swift中将UnsafeMutablePointer转换为String的正确方法是什么?

As the title says, what is the correct way to convert UnsafeMutablePointer to String in swift?

//lets say x = UnsafeMutablePointer<Int8> 

var str = x.memory.????

我尝试使用x.memory.description显然它错了,给我一个错误的字符串值。

I tried using x.memory.description obviously it is wrong, giving me a wrong string value.

推荐答案

如果指针指向以NUL结尾的UTF-8字节的C字符串,则可以执行以下操作:

If the pointer points to a NUL-terminated C string of UTF-8 bytes, you can do this:

import Foundation

let x: UnsafeMutablePointer<Int8> = ...
// or UnsafePointer<Int8>
// or UnsafePointer<UInt8>
// or UnsafeMutablePointer<UInt8>

let str = String(cString: x)

这篇关于iOS:转换UnsafeMutablePointer&lt; Int8&gt;在swift中串起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 00:47