ios - Swift 5 的 UnsafeMutablePointer 警告

标签 ios swift xcode pointers mutable

我有这个:

let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?

现在我收到警告:

Initialization of 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer') results in a dangling pointer



详细警告包括:

  1. Implicit argument conversion from '[vImagePixelCount]' (aka 'Array') to 'UnsafePointer' (aka 'UnsafePointer') produces a pointer valid only for the duration of the call to 'init(mutating:)'

  2. Use the 'withUnsafeBufferPointer' method on Array in order to explicitly convert argument to buffer pointer valid for a defined scope



有没有解决的办法?

最佳答案

尝试这个

var bytes = [UInt8]()
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: bytes.count)
uint8Pointer.initialize(from: &bytes, count: bytes.count)

关于ios - Swift 5 的 UnsafeMutablePointer 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60869370/

相关文章:

ios - SFSpeechRecognizer在线: at which url is sent the request?

objective-c - 在 Objective-C 中的循环外声明变量是否有任何优化效果?

Xcode 不自动完成 Swift 中的 Parse 函数

ios - 为什么单元测试覆盖率数据在我使用时发生变化?? (空合并运算符)?

iOS Playground 不显示 UI 预览

android - 碰撞后如何在 Lua(使用 Corona SDK)中使对象处于非 Activity 状态?

ios - 如何从 Firebase 的自定义类函数中调用数据

ios - 如何在用户行走时跟踪 GPS 坐标,来自 ios 平台的 xamarin.forms

ios - SKPhysicsBody 一侧碰撞检测

ios - 所有用户都有完全相同的日期和时间