简体   繁体   English

二进制运算符'=='不能应用于类型'UIImage?'的操作数 和“字符串”-Swift 4

[英]Binary operator '==' cannot be applied to operands of type 'UIImage?' and 'String' - Swift 4

I'm making a car quiz app where the user will try to match the model with the brand(make). 我正在制作一个汽车测验应用程序,用户将尝试将模型与品牌(制造商)进行匹配。 Both the models and brands are presented as UIImages. 模型和品牌都以UIImages的形式显示。

let carModelList = ["Camaro",
                    "Mustang",
                    "Challenger"]

let carBrandList = ["Chevy",
                    "Ford",
                    "Dodge"]

I need to check to see if the user made a correct match. 我需要检查用户是否进行了正确的匹配。 I'm doing it by comparing array indices... 我正在通过比较数组索引来做到这一点...

if carModel.image == carModelList[0] && carBrand.image == carBrandList[0] {

In this example there would be a correct match because the Camaro is made by Chevy. 在此示例中,由于Camaro由雪佛兰(Chevy)制造,因此将存在正确的匹配项。

But, I get this error... 但是,我得到这个错误...

Binary operator '==' cannot be applied to operands of type 'UIImage?' 二进制运算符'=='不能应用于类型'UIImage?'的操作数 and 'String' 和“字符串”

So, I tried changing the array to ... 因此,我尝试将数组更改为...

var carModelList: [UIImage] = [
    UIImage(named: "Camaro")!,
    UIImage(named: "Mustang")!,
    UIImage(named: "Challenger")!
]

var carBrandList: [UIImage] = [
    UIImage(named: "Chevy")!,
    UIImage(named: "Ford")!,
    UIImage(named: "Dodge")!
]

But, then at my two lines of code that randomly select the images to be presented to the user ... 但是,然后用我的两行代码随机选择要呈现给用户的图像...

carModel.image = UIImage(named: carModelList.randomElement()!)
carBrand.image = UIImage(named: carBrandList.randomElement()!)

I get this error ... 我得到这个错误...

Cannot convert value of type 'UIImage' to expected argument type 'String' 无法将类型“ UIImage”的值转换为预期的参数类型“字符串”

You're being presented with that message because indeed you're trying to pass an image, but the constructor is expecting the name of the image, which is a String. 向您显示该消息的原因是,确实确实在尝试传递图像,但是构造函数期望图像的名称为String。

if you wanna call it like this: 如果您想这样称呼它:

carModel.image = UIImage(named: carModelList.randomElement()!)
carBrand.image = UIImage(named: carBrandList.randomElement()!)

Your array has to look like this : 您的数组必须看起来像这样:

var carModelList: [String] = [
    "Camaro",
    "Mustang",
    "Challenger"
]

var carBrandList: [String] = [
    "Chevy",
    "Ford",
    "Dodge"
]

The other option is to call it like this by using your array of images: 另一个选择是通过使用图像数组来调用它:

carModel.image = carModelList.randomElement()!
carBrand.image = carBrandList.randomElement()!

Make sure to avoid force unwrapping. 确保避免强行展开。

Also, your String arrays and UIImage arrays have the same same, rename them if you wanna compare them. 另外,您的String数组和UIImage数组具有相同的名称,如果您想比较它们,请重命名它们。

The problem is that you use 问题是您使用

let carModelList = ["Camaro",
                "Mustang",
                "Challenger"]

 let carBrandList = ["Chevy",
                "Ford",
                "Dodge"]

AND

var carModelList: [UIImage] = [
    UIImage(named: "Camaro")!,
    UIImage(named: "Mustang")!,
    UIImage(named: "Challenger")!
]

var carBrandList: [UIImage] = [
    UIImage(named: "Chevy")!,
    UIImage(named: "Ford")!,
    UIImage(named: "Dodge")!
]

With same array names, that why when you do == swift can't understand which one you are using 使用相同的数组名称,这就是为什么在执行== swift时无法理解您使用的是哪一个

暂无
暂无

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

相关问题 二进制运算符&#39;!=&#39;不能应用于“ UIImage”类型的操作数 - Binary operator '!=' cannot be applied to operands of type “UIImage” 二进制运算符&#39;==&#39;不能应用于&#39;Any?&#39;类型的操作数 和&#39;String&#39;Swift iOS - Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS Swift-二进制运算符&#39;&gt; =&#39;不能应用于类型为&#39;String&#39;和&#39;Int&#39;的操作数 - Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int' Swift 二元运算符“+=”不能应用于“AnyObject!”类型的操作数和“字符串!” - Swift Binary operator '+=' cannot be applied to operands of type 'AnyObject!' and 'String!' Swift-二进制运算符不能应用于类型的操作数 - Swift - Binary operator cannot be applied to operands of type 二元运算符“+”不能应用于“_”和“String”类型的操作数 - Binary operator '+' cannot be applied to operands of type '_' and 'String' 二进制运算符不能应用于类型为Int和String的操作数-Swift 2.3-&gt; Swift 3.2转换错误 - Binary operator cannot be applied to operands of type Int and String - Swift 2.3 -> Swift 3.2 conversion error 二进制运算符“ +”不能应用于Swift中“ CGFloat”和“ CGPoint”类型的操作数 - Binary operator “+” cannot be applied to operands of type “CGFloat” and “CGPoint” in Swift 二进制运算符&#39;&gt; =&&lt;=&#39;不能应用于CGFloat和Int in Swift类型的操作数 - Binary Operator '>= & <=' cannot be applied to operands of type CGFloat and Int In Swift Swift 5:二进制运算符&#39;==&#39;不能应用于类型为&#39;AnyObject?&#39;的操作数 和“ UIView?” - Swift 5: Binary operator '==' cannot be applied to operands of type 'AnyObject?' and 'UIView?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM