简体   繁体   English

二元运算符“==”不能应用于“[String]”和“String”类型的操作数

[英]Binary operator '==' cannot be applied to operands of type '[String]' and 'String

I am new to coding and I needed some help with answering this challenge.我是编码新手,我需要一些帮助来回答这个挑战。

Instructions: 
// Given the two arrays below, write a function that takes a String as an input parameter and returns a Boolean value. The function should return true if the String input is in either array and it should return false if the String input is in neither array.
//
// Examples:
// Call your function and pass in the String "cat" as the input. Your function should return true
// Call your function and pass in the String "cow" as the input. Your function should return false

let array1 = ["dog", "cat", "bird", "pig"]
let array2 = ["turtle", "snake", "lizard", "shark"]

// Write your function below:

Here is where I have written my function:这是我编写函数的地方:

func test(Animal:String){
    let list = array1 + array2
    for animal in list {
        if list == animal {
            print("true")
        } else {
            print("false")
        }
    }
}
test(Animal: "dog")

The error I am getting is:我得到的错误是:

Binary operator '==' cannot be applied to operands of type '[String]' and 'String under the if statement.

Help if you can and I apologize in advance if this is not formatted correctly.如果可以,请提供帮助,如果格式不正确,我提前道歉。

Thanks!谢谢!

Just a tip, in the future you should try adding a more descriptive question title, that is super vague and could mean anything.只是一个提示,将来你应该尝试添加一个更具描述性的问题标题,这是非常模糊的,可能意味着任何事情。

Anyways your issue is with the line:无论如何,您的问题与线路有关:

if list == animal {

The error you got is quite specific and tells you exactly what's going wrong.您得到的错误非常具体,并准确地告诉您出了什么问题。 [String] is an array of strings while String is just a single item. [String]是一个字符串数组,而String只是一个项目。 So when you write for animal in list your taking one animal in that list at a time.因此,当您for animal in listfor animal in list写作时for animal in list您一次在该列表中选择一只动物。

Comparing an animal to a list is not allowed.不允许将动物与列表进行比较。 They are not the same type ( [String] and String are different types, as the compiler error told you).它们不是相同的类型( [String]String是不同的类型,正如编译器错误告诉您的那样)。 You can only compare variables of the same type.您只能比较相同类型的变量。 So in your case a list ( String array aka [String] ) to a another list, or an animal ( String ) to an animal.所以在你的情况下,一个列表( String array aka [String] )到另一个列表,或者一个动物( String )到一个动物。

Now what you want to do is see if a string you got is in either array.现在您要做的是查看您得到的字符串是否在任一数组中。 There's actually a built in method in arrays that lets you do exactly this: .contains数组中实际上有一个内置方法可以让你做到这一点: .contains

func test(animal:String){
    let list = array1 + array2
    if list.contains(animal) {
        print("true")
    } else {
        print("false")
    }
}

or if you want to be extra concise with some good code you can try或者如果你想用一些好的代码变得更加简洁,你可以尝试

func test(animal:String){
    let list = array1 + array2
    print(list.contains(animal)) // will be false if it doesn't so prints false, true if it does so prints true.
}

And another side note.. You want to be very careful doing an if else inside of a for loop where both the if and the else return.另一个注意事项.. 在 for 循环中执行 if else 时要非常小心,其中 if 和 else 都返回。 You shouldn't be making decisions after looking at only ONE element.您不应该只查看一个元素就做出决定。

Because typically you want to check EVERY value before printing false, but when you have an if else, you'll return/print after checking ONLY the first value.因为通常您想在打印 false 之前检查每个值,但是当您有 if else 时,您将仅在检查第一个值后返回/打印。

if you wanted to do it your way (no built in methods, iterating through the array) you would want to do something like this:如果你想按照自己的方式去做(没有内置方法,遍历数组),你会想要做这样的事情:

func test(animal:String){
    let list = array1 + array2
    for candidate in list {
        if animal == candidate { // there's a match! we can return true
            print("true ")
            return // return says to exit the function since we no longer need to keep going this is appropriate
        }
    } // for loop ends

    // we only return false once the ENTIRE for loop has run, because we know if it did exist, we would never get to this point in the code
    // this part only runs once EVERY item has been checked and none were a match
    print("false")
}

And finally as bilal says, never start variable names with capital letters.最后正如bilal所说,永远不要以大写字母开头变量名。 Typically capital letters mean its a type (like String ).通常大写字母表示它的类型(如String )。 You'll notice I renamed Animal -> animal for you in my examples你会注意到我在我的例子中为你重命名了 Animal -> 动物

暂无
暂无

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

相关问题 二元运算符“+”不能应用于“_”和“String”类型的操作数 - Binary operator '+' cannot be applied to operands of type '_' and 'String' 二元运算符“+”不能应用于“字符串”和“字符串?”类型的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String?' 二进制运算符'+'不能应用于'String'和'()-> String'类型的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and '() -> String' 二进制运算符'〜='不能应用于'String'和'String?'类型的操作数 - Binary operator '~=' cannot be applied to operands of type 'String' and 'String?' 二进制运算符“ +”不能应用于类型为“字符串”和“字符串感叹号”的操作数 - Binary operator '+' cannot be applied to operands of type 'String' and 'String exclamationmark 二进制运算符'=='不能应用于'NSObject'和'String'类型的操作数 - Binary operator '==' cannot be applied to operands of type 'NSObject' and 'String' 二进制运算符-不能应用于字符串类型的操作数! 和诠释 - Binary operator - cannot be applied to operands of type String! and Int 二进制运算符'=='不能应用于'Any?'类型的操作数 和'String'Swift iOS - Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS 二进制运算符'=='不能应用于类型'UIImage?'的操作数 和“字符串”-Swift 4 - Binary operator '==' cannot be applied to operands of type 'UIImage?' and 'String' - Swift 4 Swift-二进制运算符'> ='不能应用于类型为'String'和'Int'的操作数 - Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM