类型 'Bool' 不符合协议 'Sequence'
Type 'Bool' does not conform to protocol 'Sequence'
几周前我开始学习 Swift,在一节课(数组和 for .. in 循环)中,我不得不制作计算选票并给出答案的函数。
所以我编写这段代码时以为就是这样,但出现此错误 -> "Type 'Bool' does not conform to protocol 'Sequence'"
代码如下:
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"
}
第 4 行出现错误 'withVotes'
已经有一些数组获得了 Bool 类型的值。
编译器是对的。您正在尝试遍历一个无效的布尔值 withVotes
。
解决方案是创建一个布尔值数组。喜欢关注
for i in [true, false, true] {
if i == true { print("true") }
}
将参数 withVotes
从 Bool
更改为 [Bool]
编译器会很高兴 :)
最后可能看起来像那样
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"
}
你需要传入这样一个数组:
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"
}
欢迎学习Swift!您偶然发现了编译器正确的地方,但作为初学者,它并不总是很明显。
在这种情况下,虽然它指出第 4 行是问题所在,但这不是您需要修复的地方。您需要转到问题的 source,在本例中是第 1 行,此处...
func printResults(forIssue: String, withVotes: Bool) -> String {
具体来说withVotes: Bool
。问题是因为你写的方式,它只允许你传入一个布尔值。根据你的问题和你的其余代码,你显然想传入几个。
为此,只需将其设为布尔数组,如下所示... withVotes: [Bool]
(注意方括号。)
这是您更新后的代码,在第 1 行而不是第 4 行进行了更改。请注意,我还更新了签名和变量名称,使其更 'swifty' 如果您愿意,重点应该始终放在清晰度上:
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"
}
希望这能解释得更清楚一点,再次欢迎来到 Swift 大家庭! :)
几周前我开始学习 Swift,在一节课(数组和 for .. in 循环)中,我不得不制作计算选票并给出答案的函数。
所以我编写这段代码时以为就是这样,但出现此错误 -> "Type 'Bool' does not conform to protocol 'Sequence'"
代码如下:
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"
}
第 4 行出现错误 'withVotes'
已经有一些数组获得了 Bool 类型的值。
编译器是对的。您正在尝试遍历一个无效的布尔值 withVotes
。
解决方案是创建一个布尔值数组。喜欢关注
for i in [true, false, true] {
if i == true { print("true") }
}
将参数 withVotes
从 Bool
更改为 [Bool]
编译器会很高兴 :)
最后可能看起来像那样
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"
}
你需要传入这样一个数组:
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"
}
欢迎学习Swift!您偶然发现了编译器正确的地方,但作为初学者,它并不总是很明显。
在这种情况下,虽然它指出第 4 行是问题所在,但这不是您需要修复的地方。您需要转到问题的 source,在本例中是第 1 行,此处...
func printResults(forIssue: String, withVotes: Bool) -> String {
具体来说withVotes: Bool
。问题是因为你写的方式,它只允许你传入一个布尔值。根据你的问题和你的其余代码,你显然想传入几个。
为此,只需将其设为布尔数组,如下所示... withVotes: [Bool]
(注意方括号。)
这是您更新后的代码,在第 1 行而不是第 4 行进行了更改。请注意,我还更新了签名和变量名称,使其更 'swifty' 如果您愿意,重点应该始终放在清晰度上:
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"
}
希望这能解释得更清楚一点,再次欢迎来到 Swift 大家庭! :)