xcode 无法将“字符串”类型的值转换为预期的参数类型“布尔”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46758593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot convert value of type 'String' to expected argument type 'Bool'
提问by Anthony Rubin
I am trying to write a function that will return true if the String str starts with a vowel. the following code will compile fine
我正在尝试编写一个函数,如果 String str 以元音开头,则该函数将返回 true。以下代码将编译正常
func beginsWithVowel(str: String) -> Bool {
if(str.characters.count == 0){
return false
} else if(str.characters[str.startIndex] == "a"){
return true
}
return false
}
beginsWithVowel(str: "apple")
the problem is when I compare the first character to more than one character, for example
问题是当我将第一个字符与多个字符进行比较时,例如
else if(str.characters[str.startIndex] == "a" || "e" || "i")
then I get the error 'Cannot convert the value of type 'String' to expected argument type 'Bool''
然后我收到错误“无法将类型‘字符串’的值转换为预期的参数类型‘布尔’”
I've been fiddling with the code but no luck so far, any help would be appreciated. Thank you.
我一直在摆弄代码,但到目前为止没有运气,任何帮助将不胜感激。谢谢你。
回答by Tushar
Swift cannot infer the logic you are trying to make. The logic to Swift becomes something like this:
Swift 无法推断出您正在尝试制作的逻辑。Swift 的逻辑变成了这样:
if(str.characters[str.startIndex] == "a" || "e" || "i")
if(str.characters[str.startIndex] == "a" || "e" || "i")
is equivalent to if(<Boolean expression> || "e" || "i")
相当于 if(<Boolean expression> || "e" || "i")
is equivalent to if(<Boolean expression> || <String expression> || String expression)
相当于 if(<Boolean expression> || <String expression> || String expression)
An alternative solution can be:
另一种解决方案可以是:
if(["a", "b", "c"].contains(str.characters[str.startIndex])){
if(["a", "b", "c"].contains(str.characters[str.startIndex])){
回答by Hamza Ansari
Instead of using if else switch will be more efficient:
而不是使用 if else 开关会更有效:
func beginsWithVowel(str: String) -> Bool {
guard str.characters.count > 0 else {
return false
}
switch str.characters[str.startIndex]{
case "a","e","i","o","u":
return true
default:
return false
}
}
回答by Dariusz Bukowski
You should write it like this:
你应该这样写:
else if(str.characters[str.startIndex] == "a" || str.characters[str.startIndex] == "e" || str.characters[str.startIndex] == "i")
You get the error, because the compiler tries to convert both "e" and "i" to type Bool.
您会收到错误消息,因为编译器尝试将“e”和“i”都转换为 Bool 类型。
回答by Haroldo Gondim
When you perform "a" || "e" || "i"
you are comparing between the strings
. Use this code:
当您执行时,"a" || "e" || "i"
您正在比较strings
. 使用此代码:
if(str.characters[str.startIndex] == "a"
|| str.characters[str.startIndex] == "e"
|| str.characters[str.startIndex] == "i") {
// Your Code...
}
回答by vadian
The boolean OR operator ||
expects boolean expressions.
布尔 OR 运算符||
需要布尔表达式。
So you would have to write EXPR == "a" || EXPR == "e" || EXPR == "i"
where EXPR
is the expression to get the first character.
因此,您必须编写获取第一个字符的表达式EXPR == "a" || EXPR == "e" || EXPR == "i"
where EXPR
。
However there is an easier solution (code is Swift 4)
但是有一个更简单的解决方案(代码是 Swift 4)
func beginsWithVowel(str: String) -> Bool {
return "aeiou".contains(String(str.prefix(1)))
}
It considers also the empty string case.
它还考虑空字符串的情况。