简体   繁体   中英

How can I compare a boolean to an array? - Swift

I have this:

if sender.tag == 1 || sender.tag == 2 || sender.tag == 3 || sender.tag == 4 || sender.tag == 5 || sender.tag == 6 || sender.tag == 7 || sender.tag == 8 || sender.tag == 9 || sender.tag == 10 {

Is there anyway to minimize this some similar to this:

if sender.tag == [1 || 2 || 3 || 4 || 5 || 6 ||  7 || 8 || 9 || 10] {

How about

if (1...10).contains(sender.tag)

?

You could do something like:

if 1 <= sender.tag && sender.tag <= 10 {

This checks if sender.tag is between 1 and 10 inclusive.

You can use a set or range:

let tagsSet: Set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

if tagsSet.contains(sender.tag)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM