简体   繁体   English

类型“布尔”不符合协议“序列”

[英]Type 'Bool' does not conform to protocol 'Sequence'

I started learning Swift few weeks ago and in one lesson (Arrays and for .. in loops) I had to make func that counts votes and gives an answer. 我几周前开始学习Swift,在一堂课(数组和for .. in循环)中,我不得不做些能计算票数并给出答案的函数。

So I made this code thinking thats it but this error pops in -> "Type 'Bool' does not conform to protocol 'Sequence'" 因此,我使这段代码以为是这样,但是此错误在->“类型'Bool'不符合协议'Sequence'”中弹出

here's the code: 这是代码:

func printResults(forIssue: String, withVotes: Bool) -> String {
    positive = 0
    negative = 0
    for votes in withVotes {
        if votes == true {
            positive += 1
        } else {
            negative += 1
        }
    }
    return "\(forIssue) \(positive) yes, \(negative) no"
}

The error pops in 4th line with 'withVotes' 错误在第4行显示“ withVotes”

There are already some arrays that got Bool type values. 已经有一些具有Bool类型值的数组。

Welcome to learning Swift! 欢迎学习Swift! You've stumbled across something where the compiler is right, but as a beginner, it's not always evident on what's going on. 您偶然发现了编译器正确的地方,但是作为一个初学者,并不总是清楚所发生的事情。

In this case, although it's pointing to line 4 as the problem, that's not where you need to fix it. 在这种情况下,尽管它指向第4行是问题,但并不是您需要解决的地方。 You need to go to the source of the problem, which in this case is line 1, here... 您需要转到问题的根源 ,在这种情况下,这里是第1行...

func printResults(forIssue: String, withVotes: Bool) -> String {

Specifically withVotes: Bool . 特别是withVotes: Bool The problem is because of the way you have it written, it's only allowing you to pass in a single boolean. 问题是由于您编写的方式,它只允许您传递单个布尔值。 By your question and the rest of your code, you clearly want to pass in several. 通过您的问题和其余代码,您显然希望传递几个。

To do that, simply make it a bool array, like this... withVotes: [Bool] (Note the square brackets.) 为此,只需将其设置为布尔数组,例如... withVotes: [Bool] (请注意方括号。)

Here's your updated code with the change on line 1, not line 4. Note I also updated the signature and variable names to be more 'swifty' if you will where the focus should always be on clarity: 这是您的更新后的代码,其中第1行(而不是第4行)有所更改。请注意,如果您将重点始终放在清晰度上,我还更新了签名和变量名,使其更加“敏捷”:

func getFormattedResults(for issue: String, withVotes allVotes: [Bool]) -> String {

    var yesVotes = 0
    var noVotes  = 0

    for vote in allVotes {
        if vote {
            yesVotes += 1
        }
        else {
            noVotes += 1
        }
    }

    return "\(issue) \(yesVotes) yes, \(noVotes) no"
}

Hope this explains it a little more, and again, welcome to the Swift family! 希望这能进一步解释它,再次欢迎您来到Swift家族! :) :)

The compiler is right. 编译器是正确的。 You're trying to iterate through a bool value withVotes which will not work. 您正在尝试使用withVotes遍历bool值。

The solution is to create an array of bool values. 解决方案是创建布尔值数组。 Like following 像下面

for i in [true, false, true] {
    if i == true { print("true") }

}

Change your parameter withVotes from Bool to [Bool] and the compiler will be happy :) withVotes的参数从Bool更改为[Bool] ,编译器会很高兴:)

At the end and probably will look like that 最后,可能看起来像

func printResults(forIssue: String, withVotes: [Bool]) -> String {
    positive = 0
    negative = 0
    for votes in withVotes {
        if votes == true {
            positive += 1
        } else {
            negative += 1
        }
    }
    return "\(forIssue) \(positive) yes, \(negative) no"
}

You need to pass in an array like this: 您需要像这样传递一个数组:

func printResults(forIssue: String, withVotes: [Bool]) -> String {
    positive = 0
    negative = 0
    for votes in withVotes {
        if votes == true {
            positive += 1
        } else {
            negative += 1
        }
    }
    return "\(forIssue) \(positive) yes, \(negative) no"
}

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

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