UnsafeMutablePointer Warning with Swift 5

前端 未结 3 1343
眼角桃花
眼角桃花 2020-12-06 23:34

I had this:

let alphaPtr = UnsafeMutablePointer(mutating: alpha) as UnsafeMutablePointer?

W

3条回答
  •  执念已碎
    2020-12-06 23:35

    It was never safe to do this, and the compiler now is warning you more aggressively.

    let alphaPtr = UnsafeMutablePointer ...
    

    At the end of this line, alphaPtr is already invalid. There is no promise that what it points to is still allocated memory.

    Instead, you need to nest whatever usage you need into a withUnsafeMutablePointer() (or withUnsafePointer()) block. If you cannot nest it into a block (for example, if you were storing the pointer or returning it), there is no way to make that correct. You'll have to redesign your data management to not require that.

提交回复
热议问题