iOS:在swift中将UnsafeMutablePointer转换为String?

前端之家收集整理的这篇文章主要介绍了iOS:在swift中将UnsafeMutablePointer转换为String?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
正如标题所说,在 swift中将UnsafeMutablePointer转换为String的正确方法是什么?
//lets say x = UnsafeMutablePointer<Int8> 

var str = x.memory.????

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

解决方法

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

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

let str = String(cString: x)
原文链接:https://www.f2er.com/iOS/333086.html

猜你在找的iOS相关文章