将 obj-c 代码转换为 swift 2 块问题



我在目标C中有以下代码。

[pixelData processPixelsWithBlock:^(JGPixel *pixel, int x, int y) {
}];

现在,对我来说,SWIFT 等价物将是

pixelData.processPixelsWithBlock({(pixel: JGPixel, x: Int, y: Int) -> Void in
})

但是,这抛出了一个错误

无法将类型"(JGPixel, Int, Int) -> Void" 的值转换为预期的参数类型"((UnsafeMutablePointer, Int32, Int32) -> Void)!"

任何人都可以帮助解释我出错的地方,以及我如何了解有关此错误的更多信息。谢谢!

您需要

使用此处所述的withUnsafeMutablePointer函数。大致如下:

var pixel: JGPixel = // whatever
withUnsafeMutablePointer(&pixel, { (ptr: UnsafeMutablePointer<JGPixel>) -> Void in
    pixelData.processPixelsWithBlock({(ptr, x: Int, y: Int) -> Void in
    })
})

编辑:刚刚意识到我忘了正确传递ptr到函数。上面固定的。

最新更新