简体   繁体   English

无法将“字符串”类型的值转换为预期的参数类型“布尔”

[英]Cannot convert value of type 'String' to expected argument type 'Bool'

I am trying to write a function that will return true if the String str starts with a vowel. 我正在尝试编写一个函数,如果String str以元音开头,该函数将返回true。 the following code will compile fine 以下代码将编译正常

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'' 然后我得到错误“无法将类型'String'的值转换为预期的参数类型'Bool'

I've been fiddling with the code but no luck so far, any help would be appreciated. 我一直在摆弄代码,但到目前为止没有运气,任何帮助将不胜感激。 Thank you. 谢谢。

Swift cannot infer the logic you are trying to make. Swift无法推断您要尝试的逻辑。 The logic to Swift becomes something like this: Swift的逻辑如下所示:

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])){

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类型。

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
    }
}

When you perform "a" || "e" || "i" 当您执行"a" || "e" || "i" "a" || "e" || "i" "a" || "e" || "i" you are comparing between the strings . "a" || "e" || "i"您在strings之间进行比较。 Use this code: 使用此代码:

if(str.characters[str.startIndex] == "a" 
    || str.characters[str.startIndex] == "e" 
    || str.characters[str.startIndex] == "i") {

    // Your Code...

}

The boolean OR operator || 布尔OR运算符|| expects boolean expressions. 需要布尔表达式。

So you would have to write EXPR == "a" || EXPR == "e" || EXPR == "i" 因此,您将不得不编写EXPR == "a" || EXPR == "e" || EXPR == "i" EXPR == "a" || EXPR == "e" || EXPR == "i" EXPR == "a" || EXPR == "e" || EXPR == "i" where EXPR is the expression to get the first character. EXPR == "a" || EXPR == "e" || EXPR == "i"其中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. 它还考虑空字符串的大小写。

暂无
暂无

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

相关问题 无法将Int类型的值转换为预期的参数类型Bool - Cannot convert value of type Int to expected argument type Bool 无法将类型&#39;(_)-&gt;()&#39;的值转换为预期的参数类型&#39;((Bool,Error?)-&gt; Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?' 无法将“KotlinBoolean”类型的值转换为预期的参数类型“Bool” - Cannot convert value of type 'KotlinBoolean' to expected argument type 'Bool' 无法将类型&#39;(_) - &gt; Bool&#39;的值转换为预期的参数类型&#39;NSPredicate&#39; - Cannot convert value of type '(_) -> Bool' to expected argument type 'NSPredicate' Swift:无法将类型&#39;() - &gt; Bool&#39;的值转换为预期的参数类型&#39;PFObject&#39; - Swift: Cannot convert value of type '() -> Bool' to expected argument type 'PFObject' 无法将类型()的值转换为预期的参数类型bool(Swift)? - Cannot convert value of type () to expected argument type bool (Swift)? Swift:无法将&#39;String&#39;类型的值转换为预期的参数类型&#39;@noescape(AnyObject)引发-&gt; Bool&#39; - Swift: Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool' 无法将类型&#39;String?.Type&#39;的值转换为预期的参数类型&#39;String?&#39; - Cannot convert value of type 'String?.Type' to expected argument type 'String?' 无法转换“已发布”类型的值<bool> .Publisher'到预期的参数类型'绑定<bool> '</bool></bool> - Cannot convert value of type 'Published<Bool>.Publisher' to expected argument type 'Binding<Bool>' 无法将类型“ [String:Any]”的值转换为预期的参数类型“ String” - Cannot convert value of type '[String : Any]' to expected argument type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM