在Swift中比较字符串

mva*_*sco 1 ios swift

我正在使用以下代码比较两个字符串,并根据比较显示不同的图像:

let sameQuotation = "0"
        if prioridad == sameQuotation {
            print("es 0-")
            let image11 = UIImage(named: "pri0") as UIImage?
        }


        let sameQuotation1 = "1"
        if prioridad == sameQuotation1 {
            print("es 1-")
            let image11 = UIImage(named: "pri1") as UIImage?
        }
Run Code Online (Sandbox Code Playgroud)

“打印”操作可以完美完成,但是图像不会改变。

我是Swift的新手,可能我的代码有问题。

谢谢。

Lyn*_*ott 5

它没有改变,因为您要image11在每个条件中重新声明。更改let image11 = ...image11 = ...访问我假设的先前声明的版本image11,例如:

if prioridad == sameQuotation {
    print("es 0-")
    image11 = UIImage(named: "pri0") as UIImage?
}


let sameQuotation1 = "1"
if prioridad == sameQuotation1 {
    print("es 1-")
    image11 = UIImage(named: "pri1") as UIImage?
}
Run Code Online (Sandbox Code Playgroud)