繁体   English   中英

Swift:For-in 循环需要“[DeepSpeechTokenMetadata]”以符合“Sequence”

[英]Swift: For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence'

我遇到了一个带有 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 {
}

暂无
暂无

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

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