首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift: For-in循环要求'[DeepSpeechTokenMetadata]‘符合'Sequence’

Swift: For-in循环要求'[DeepSpeechTokenMetadata]‘符合'Sequence’
EN

Stack Overflow用户
提问于 2020-08-27 12:39:04
回答 1查看 4.5K关注 0票数 2

我遇到了一个奇怪的错误,一个for in循环和一个数组。上面写着

代码语言:javascript
运行
复制
For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence'

这没有任何意义。它知道这是一个数组。

有问题的for循环:

代码语言:javascript
运行
复制
      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()
        }

下面是包含我试图遍历的数组的类的定义:

代码语言:javascript
运行
复制
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
    }
}

谢谢!

EN

Stack Overflow用户

回答已采纳

发布于 2020-08-27 13:04:11

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

代码语言:javascript
运行
复制
for (x, token) in transcriptCandidate.enumerated() {
}

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

代码语言:javascript
运行
复制
for token in transcriptCandidate {
}
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63609283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档