【问题标题】:Swift: For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence'Swift:For-in 循环要求“[DeepSpeechTokenMetadata]”符合“序列”
【发布时间】:2020-12-15 22:35:51
【问题描述】:

我在 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
    }
}

谢谢!

【问题讨论】:

  • decoded.transcripts[0].tokens - 它是什么?

标签: swift for-in-loop


【解决方案1】:

您可以这样做,其中x 是索引,token 是元素:

for (x, token) in transcriptCandidate.enumerated() {
}

如果您不需要索引,也可以这样做:

for token in transcriptCandidate {
}

【讨论】:

    猜你喜欢
    • 2023-01-08
    • 1970-01-01
    • 2020-07-22
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多