1

Currently in swift we can check instance's class type by using 'is' operator. for example:

....
guard anyInstance is MYClass else {
    return
}
....

In my case, I need to compare same instance with multiple class type, for example:

...
    if anyInstance is MyClassOne ||
          anyInstance is MyClassTwo || anyInstance is MyClassThree {
          return
        }
...

Is there a better way in swift to write this kind for conditions? something like

....

if anyInstance is (MyClassOne, MyClassTwo, MyClassThree) {
return
}
...
James Z
  • 12,209
  • 10
  • 24
  • 44
idev
  • 311
  • 3
  • 10
  • 2
    Why would you want to check if that? How does it help to know if an object is an instance of one of three possible classes? In order to call any specific methods on the object you have to (conditionally) cast it anyway. – Perhaps you want to define a common superclass or a protocol to which those classes conform? – Martin R May 01 '18 at 10:16
  • You can use `case is MyClass` in a switch statement, but the suggestion from @MartinR to use a common superclass is good. – Chris May 01 '18 at 10:34
  • Hi Martin, Thanks for your reply. In my case, 'anyInstance' is of the type UIViewController and I don't want to call dismiss() method if 'anyInstance' is kind of type MyClass*. – idev May 01 '18 at 10:35
  • If `anyInstance` is supposed to be `UIViewController` why don't you check `... is UIViewController` rather than `is not A || is not B || is not C` ?? – vadian May 01 '18 at 14:49

3 Answers3

1

If I think about it logically, it would make a lot more sense to define a superclass / protocol for them, something like this:

class MyClassNumber { }

class MyClassOne: MyClassNumber { }
class MyClassTwo: MyClassNumber { }
class MyClassLetter { }

let one = MyClassOne()
let two = MyClassTwo()
let letter = MyClassLetter()

if one is MyClassNumber {
    // TRUE
}

if two is MyClassNumber {
    // TRUE
}

if letter is MyClassLetter {
    // FALSE
}

Don't see any use case for yours

Bence Pattogato
  • 3,752
  • 22
  • 30
0

As per your comment, It looks like you need it to check if your anyInstance is from a list of UIViewController then you don't want to call dismiss():

There are no any method, where you can pass the array as:

if anyInstance is (MyClassOne, MyClassTwo, MyClassThree) {
    return
}

But, You can use the switch statement to perform this as:

suppose anyInstance is your class object

    switch anyInstance {
    case is MyClassOne, is MyClassTwo ,is MyClassThree:
        print("one is either from MyClassOne, MyClassTwo, MyClassThree")
    default:
        break
    }
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
-1
class MyClassOne: MyClassNumber { }
class MyClassTwo: MyClassNumber { }
class MyClassThree: MyClassNumber { }

Inherit all class from one class and than check after that like

    if anyInstance is BaseClass{
    }
CrazyPro007
  • 1,006
  • 9
  • 15
  • Thanks for answering! But please take a few minutes to read [how to answer](https://stackoverflow.com/help/how-to-answer) to get some useful tips on answering questions. Esp. the part "fuller explanations are better". – Gert Arnold May 13 '18 at 11:03