Swift: цикл For-in требует «[DeepSpeechTokenMetadata]» для соответствия «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
    }
}

Спасибо!


person Ryan Tremblay    schedule 27.08.2020    source источник
comment
decoded.transcripts[0].tokens — что это такое?   -  person Roman Ryzhiy    schedule 27.08.2020


Ответы (1)


Вы можете сделать это, где x — индекс, а token — элемент:

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

Или это, если вам не нужен индекс:

for token in transcriptCandidate {
}
person Rob    schedule 27.08.2020