检查数组中的Int.Swift 3,iOS

Tob*_* V. 0 int sender ios swift

我有以下代码:

print (sender.tag) // prints 21  (example)
print(DictPl2) // prints [0, 1, 20, 21, 84, 94, 26, 27, 37, 55, 56, 66, 52, 53, 54, 55, 72, 73, 74, 75]
var DictPl2 = [Int]()
if playeractive == 1 {
 for i in DictPl2 {
                if i == sender.tag {
                    print("Well done")
                    sender.backgroundColor = UIColor.red

                }

                else {    //always goes this
                    print("Bad")
                    sender.backgroundColor = UIColor.brown
                }
            }
Run Code Online (Sandbox Code Playgroud)

我正在检查sender.tag是否与数组DictPl2中的任何元素相等.但如果它相等或不相等,代码总是以标记的方式进行.谁知道,可能是什么错误?谢谢

Ste*_*nov 5

你可以用DictPl2.contains(sender.tag)而不是你的for loop.然后你的代码看起来像:

if DictPL2.contains(sender.tag) {
    print("Well done")
    sender.backgroundColor = UIColor.red
} else {
    print("Bad")
    sender.backgroundColor = UIColor.brown
}
Run Code Online (Sandbox Code Playgroud)