![](/img/trans.png)
[英]For-in loop requires '[UserVehicles]?' to conform to 'Sequence'; did you mean to unwrap optional? Swift
[英]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.