gpt4 book ai didi

arrays - 数组创建导致 'Type of expression is ambiguous without more context'

转载 作者:行者123 更新时间:2023-11-30 11:57:04 25 4
gpt4 key购买 nike

我对 Swift 很陌生。如果我的问题听起来很愚蠢,请原谅。

我声明了一个结构如下:

struct RecorderState 
{
var setupComplete: Bool
var dataFormat: AudioStreamBasicDescription
var queue: UnsafeMutablePointer<AudioQueueRef>
var buffers: [AudioQueueBufferRef]
var recordFile: AudioFileID
var bufferByteSize: UInt32
var currentPacket: Int64
var isRunning: Bool
var recordPacket: Int64
var errorHandler: ((NSError) -> Void)?
}

现在我需要编写类的 init 方法来初始化其中的结构。

class SimplePCMRecorder : NSObject
{
private var recorderState: RecorderState

init(numberBuffers: Int)
{
self.recorderState = RecorderState(
setupComplete: false,
dataFormat: AudioStreamBasicDescription(),
queue: UnsafeMutablePointer<AudioQueueRef>.alloc(1),
buffers: Array<AudioQueueBufferRef>(repeating: 0, count: numberBuffers),
recordFile:AudioFileID(),
bufferByteSize: 0,
currentPacket: 0,
isRunning: false,
recordPacket: 0,
errorHandler: nil
)
}
}

问题是,错误“表达式类型在没有更多上下文的情况下不明确”继续显示在 buffers: Array<AudioQueueBufferRef>(repeating: 0, count: numberBuffers) 行上。

enter image description here

我尝试了其他方法,但还是一样。

enter image description here

从我在这里读到的内容 Error: 'Type of expression is ambiguous without more context'是编译器不知道对象的类型,因此它显示错误。但在这里,我已经声明了。

我尝试通过在构造函数方法后面添加 as 来再次转换它,但仍然不起作用。

enter image description here

我不明白为什么。我已将缓冲区变量的类型声明为 Array包含AudioQueueBufferRef 。但为什么编译器仍然无法检测到这一点?

最佳答案

由于 Martin R's上面评论。

class SimplePCMRecorder : NSObject
{
private var recorderState: RecorderState
init(numberBuffers: Int)
{

self.recorderState = RecorderState(
setupComplete: false,
dataFormat: AudioStreamBasicDescription(),
queue: UnsafeMutablePointer<AudioQueueRef>.allocate(capacity: 1),
buffers: Array<UnsafeMutablePointer<AudioQueueBuffer>>(repeating: UnsafeMutablePointer<AudioQueueBuffer>(bitPattern: 0)! , count: numberBuffers),
recordFile:AudioFileID(bitPattern: 0)!,
bufferByteSize: 0,
currentPacket: 0,
isRunning: false,
recordPacket: 0,
errorHandler: nil
)
}
}

关于arrays - 数组创建导致 'Type of expression is ambiguous without more context',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47690452/

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