swift - Swift:For-in 循环要求“[DeepSpeechTokenMetadata]”符合“序列”
问题描述
我在 for in 循环和数组中遇到了一个奇怪的错误。它说
For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence'
这没有任何意义......它知道它是一个数组......
有问题的 for 循环:
var transcriptCandidate = decoded.transcripts[0].tokens
var words = [String]()
var timestamps = [Int]()
var workingString = ""
var lastTimestamp = -1
for (x, token) in transcriptCandidate {
let text = token.text
let timestamp = token.startTime
if(lastTimestamp == -1){
lastTimestamp = timestamp.toInt()
}
这是包含我要遍历的数组的类的定义:
public struct DeepSpeechCandidateTranscript {
/// Array of DeepSpeechTokenMetadata objects
public private(set) var tokens: [DeepSpeechTokenMetadata] = []
/** Approximated confidence value for this transcript. This corresponds to
both acoustic model and language model scores that contributed to the
creation of this transcript.
*/
let confidence: Double
internal init(fromInternal: CandidateTranscript) {
let tokensBuffer = UnsafeBufferPointer<TokenMetadata>(start: fromInternal.tokens, count: Int(fromInternal.num_tokens))
for tok in tokensBuffer {
tokens.append(DeepSpeechTokenMetadata(fromInternal: tok))
}
confidence = fromInternal.confidence
}
}
谢谢!
解决方案
您可以这样做,x
索引在哪里token
,元素在哪里:
for (x, token) in transcriptCandidate.enumerated() {
}
或者,如果您不需要索引,请执行以下操作:
for token in transcriptCandidate {
}
推荐阅读
- xml - 如何使用 XML::Simple 根据 Perl 中的 XML 属性内容修改 XML 元素?
- visual-studio-code - 自动格式化,使用 Visual Studio Code 自动换行(在 Mac 上)
- outlook - UiPath StudioX - Outlook 桌面应用程序 COM 加载项错误
- python - tkinter 无法识别图像数据
- android - 如何更改缺少中间名的android包名称?
- php - 如何将加载的自定义计费字段保存到 woocommerce 订单元(管理员订单)?
- c++ - 错误:构建包 ncurses:x86-windows 失败:BUILD_FAILED
- redux - 在 redux 或 react-redux 中面临登录页面问题
- c# - 使用 mpxj 从 mpp 文件中读取任务企业自定义字段值
- python - Tensorflow:如何将注意力层的输出作为输入参数返回?