繁体   English   中英

无法为类型&#39;UnsafeMutablePointer调用初始化程序 <Float> &#39;,其参数类型为&#39;(UnsafeMutableRawPointer!)&#39;

[英]Cannot invoke initializer for type 'UnsafeMutablePointer<Float>' with an argument list of type '(UnsafeMutableRawPointer!)'

第5行的关联对应用程序的成功没有帮助。 但老实说,我不知道那时会发生什么。

ÜnsafeMutablePointer的重载具有部分匹配的参数列表:(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)
        }

您可以找到一些类似的文章,搜索错误消息。

例如: 如何在Swift 3中使用UnsafeMutablePointer?

而且您最好阅读“ 迁移到SWIFT 3” ,特别是这篇文章


具体到您的情况。

更改此行:

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 ...}中的闭包{(floats: UnsafeMutablePointer<Float>) in ...} 。)

let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
var buf = unsafeBitCast(baseAddress, to: UnsafeMutablePointer<UInt8>.self)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM