简体   繁体   中英

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

I'm running into a weird error with a for in loop and an array. it says

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

Which doesn't make any sense... it knows it's an Array...

The for loop in question:

      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()
        }

Here's the definition of the class that contains the array I'm trying to iterate through:

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

Thanks!

You can either do this, where x is the index and token is the element:

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

Or this if you don't need the index:

for token in transcriptCandidate {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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