UnsafeMutableRawPointer

UnsafeMutableRawPointer

本文介绍了收到类型为"UnsafeMutableRawPointer"的错误消息,没有下标成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要分配内存&在那里设置值.这是我的代码.

i just need to alloc memory & set values there.Here is my code.

 let radius:Float = 5.79
 let sigma:Float = radius / 2
 let size:Int = Int((round(radius) * 2) + 1)

 var weights:UnsafeMutableRawPointer = malloc(MemoryLayout<Float>.size * size * size)

 weights[some index] = some vale

但是我在快速版本4中收到一条错误消息,提示"UnsafeMutableRawPointer没有下标成员"

but I am getting an error message in swift version 4 saying "UnsafeMutableRawPointer has no subscript members"

我该如何解决这个问题.请提供任何想法

How can I fixed this.Any ideas please

推荐答案

您最好检查 UnsafeMutableRawPointer

You should better check the official documentation of UnsafeMutableRawPointer

您可以这样写:

let radius:Float = 5.79
let sigma:Float = radius / 2
let size:Int = Int((round(radius) * 2) + 1)

var weights:UnsafeMutableRawPointer = malloc(MemoryLayout<Float>.size * size * size)

weights.storeBytes(of: some value, toByteOffset: some offset, as: SomeType.self)

或者,如果所有元素都是 Float ,则最好改用 UnsafeMutablePointer< Float> .

Or you should better use UnsafeMutablePointer<Float> instead, if all the elements are Float.

var weights:UnsafeMutablePointer<Float> = UnsafeMutablePointer.allocate(capacity: size * size)

weights[some index] = some vale

这篇关于收到类型为"UnsafeMutableRawPointer"的错误消息,没有下标成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:10