gpt4 book ai didi

ios - 无法使用类型为 'UnsafeMutablePointer' 的参数列表调用类型为 '(UnsafeMutableRawPointer!)' 的初始值设定项

转载 作者:行者123 更新时间:2023-11-28 09:25:42 24 4
gpt4 key购买 nike

第 5 行的这种关联对应用程序的成功没有贡献。但老实说,我不明白那一刻发生了什么。

Overloads for ÜnsafeMutablePointer exist with partially matching parameter list: (RawPointer).

但这是什么意思呢?谢谢

override func buffer(withCsound cs: CsoundObj) -> Data {
let length = Int(AKSettings.shared().numberOfChannels) *
Int(AKSettings.shared().samplesPerControlPeriod) * 4
let num = length / 4
let floats = UnsafeMutablePointer<Float>(malloc(length))

/* The phase and amplitude are different for each line to get a nice
* gimmick. */
let phase = (self.amplifier + 0.8) / 1.8

for i in 0 ... num - 1 {
/* The amplitude is placed within the for-loop because it can fade
* to a slightly different value during one plot refresh. */
let amplitude = self.amplifier * self.amplitude

/* It is incredibly important that `time` and `phase` aren't
* multiplied with the frequency or else it will bump at each
* frequency change. */
var t = (time + Double(i) / Double(num) * self.frequency + phase)

floats[i] = Float(sin(t * 2 * 3.14))

/* It is multiplied with a "regular" 0.5 Hz sine to get both ends
* to fade out nicely. It's sort of a simplistic window function. */
t = Double(i) / Double(num)
floats[i] *= Float(sin(t * 1 * 3.14) * amplitude)
floats[i] *= 1 - pow(1 - Float(i) / Float(num), 2.0)

time += self.frequency / 44100 / 2

/* Fade smoothly to the next frequency and amplitude. */
self.frequency += (nextFrequency - self.frequency) / 44100.0 / 4.0
self.amplitude += (nextAmplitude - self.amplitude) / 44100.0 / 2.0
}

/* We keep the time between 0 and 1 to make sure it never overflows /
* loses the necessary precision. */
time = fmod(time, 1.0)

return Data(bytesNoCopy: UnsafeMutablePointer<UInt8>(floats), count: length, deallocator: .free)
}

最佳答案

你可以找到一些类似的文章搜索错误信息。

例如:How to use UnsafeMutablePointer in Swift 3?

你最好阅读MIGRATING TO SWIFT 3 , 特别是 this article .


具体到您的情况。

改变这一行:

let floats    = UnsafeMutablePointer<Float>(malloc(length))

到:

let rawBytes  = malloc(length)!
let floats = rawBytes.assumingMemoryBound(to: Float.self)

并更改最后一行:

return Data(bytesNoCopy: UnsafeMutablePointer<UInt8>(floats), count: length, deallocator: .free)

到:

return Data(bytesNoCopy: rawBytes, count: length, deallocator: .free)

选项 2。

更改行 let floats = UnsafeMutablePointer<Float>(malloc(length))到:

var data = Data(count: length)
data.withUnsafeMutableBytes {(floats: UnsafeMutablePointer<Float>) in

并将最后一行更改为:

}
return data

(它们之间的所有行都包含在闭包 {(floats: UnsafeMutablePointer<Float>) in ...} 中。)

关于ios - 无法使用类型为 'UnsafeMutablePointer<Float>' 的参数列表调用类型为 '(UnsafeMutableRawPointer!)' 的初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41517732/

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