gpt4 book ai didi

swift - 将 UnsafeMutablePointer 类型转换为 UnsafeMutablePointer<#Struct type#>

转载 作者:IT王子 更新时间:2023-10-29 05:40:29 28 4
gpt4 key购买 nike

我在 Swift 中创建了一个名为 RGB 的结构,非常简单:

struct PixelRGB {
var r: CUnsignedChar = 0
var g: CUnsignedChar = 0
var b: CUnsignedChar = 0

init(red: CUnsignedChar, green: CUnsignedChar, blue: CUnsignedChar) {
r = red
g = green
b = blue
}
}

我有一个指针 var imageData: UnsafeMutablePointer<PixelRGB>! .

我想为这个指针分配一些空间,但是malloc返回 UnsafeMutablePointer<Void>我不能像下面这样转换它:

imageData = malloc(UInt(dataLength)) as UnsafeMutablePointer<PixelRGB> // 'Void' is not identical to `PixelRGB`

无论如何要解决这个问题?感谢您的帮助!

最佳答案

我想你想说的大概是这样的:

imageData = UnsafeMutablePointer<PixelRGB>.alloc(dataLength)

关于swift - 将 UnsafeMutablePointer<Void> 类型转换为 UnsafeMutablePointer<#Struct type#>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26006710/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com