Arrays 类型';布尔';不符合协议';序列';
几周前我开始学习Swift,在一节课(数组和for..in循环)中,我必须制作计算选票并给出答案的func 所以我让这段代码认为是这样的,但是这个错误出现在->“类型'Bool'不符合协议'Sequence'” 代码如下:Arrays 类型';布尔';不符合协议';序列';,arrays,swift,for-in-loop,Arrays,Swift,For In Loop,几周前我开始学习Swift,在一节课(数组和for..in循环)中,我必须制作计算选票并给出答案的func 所以我让这段代码认为是这样的,但是这个错误出现在->“类型'Bool'不符合协议'Sequence'” 代码如下: func printResults(forIssue: String, withVotes: Bool) -> String { positive = 0 negative = 0 for votes in withVotes {
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类型值 编译器是对的。您正在尝试使用投票权迭代布尔值
,但该操作无效
解决方案是创建一个布尔值数组。像下面
for i in [true, false, true] {
if i == true { print("true") }
}
将参数withvows
从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"
}
编译器是对的。您正在尝试使用投票权迭代布尔值,但该操作无效
解决方案是创建一个布尔值数组。像下面
for i in [true, false, true] {
if i == true { print("true") }
}
将参数withvows
从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"
}
您需要传入如下数组:
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行是问题所在,但这不是需要解决的地方。您需要找到问题的根源,在本例中是第1行,这里
func printResults(forIssue: String, withVotes: Bool) -> String {
特别是投票:Bool
。问题在于您编写它的方式,它只允许您传入一个布尔值。根据您的问题和代码的其余部分,您显然希望传递几个
要做到这一点,只需将其设置为布尔数组,如下所示<代码>带投票:[Bool]
(注意方括号。)
这是您在第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"
}
希望这能解释得更清楚一点,再一次,欢迎来到斯威夫特家族 欢迎学习Swift!您偶然发现了一些编译器是正确的地方,但作为初学者,它并不总是对正在发生的事情很清楚
在本例中,虽然它指出第4行是问题所在,但这不是需要解决的地方。您需要找到问题的根源,在本例中是第1行,这里
func printResults(forIssue: String, withVotes: Bool) -> String {
特别是投票:Bool
。问题在于您编写它的方式,它只允许您传入一个布尔值。根据您的问题和代码的其余部分,您显然希望传递几个
要做到这一点,只需将其设置为布尔数组,如下所示<代码>带投票:[Bool]
(注意方括号。)
这是您在第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"
}
希望这能解释得更清楚一点,再一次,欢迎来到斯威夫特家族
WithVoces
目前不是数组,也许你是故意的?错误信息非常清楚:Swift需要一个序列
,而你给了它一个Bool
。给它一个序列,它会很高兴:)。withvows
目前不是数组,也许你是故意的?错误信息很清楚:Swift需要一个序列,而你给了它一个Bool
。给它一个序列,它会很高兴:)。