简体   繁体   English

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

[英]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.我遇到了一个带有 for in 循环和数组的奇怪错误。 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:有问题的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()
        }

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:您可以这样做,其中x是索引, token是元素:

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

Or this if you don't need the index:或者,如果您不需要索引:

for token in transcriptCandidate {
}

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

相关问题 For-in 循环需要“[UserVehicles]?” 符合“顺序”; 你的意思是解开可选的? 迅速 - For-in loop requires '[UserVehicles]?' to conform to 'Sequence'; did you mean to unwrap optional? Swift For-in 循环需要“JSON?” 符合“顺序”; 你的意思是解开可选的吗? - For-in loop requires 'JSON?' to conform to 'Sequence'; did you mean to unwrap optional? “for-in 循环需要 '[String]?' 符合“顺序”; 你的意思是解开可选的包装吗?” 但我不认为我在使用选项 - “For-in loop requires '[String]?' to conform to 'Sequence'; did you mean to unwrap optional?” but I don't think I am using optionals Swift JSON for-in循环 - Swift JSON for-in loop 由于类型String不符合协议IntervalType而导致Swift for-in循环错误 - Swift for-in loop error as type String doesn't conform to protocol IntervalType Swift for-in循环字典实验 - Swift for-in loop dictionary experiment for-in循环中的索引范围-Swift - Scope of index in for-in loop - swift 如何符合Swift 4中的序列协议 - How To Conform to the Sequence Protocol in Swift 4 实例方法“items”要求“listen”符合“sequence” - Instance method 'items' requires that 'listen' conform to 'Sequence' 使用for-in循环快速替换数组中的字符串 - Swift replace string in array using for-in loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM