0

I have model for my class that may be instance of 2 different classes. Now i check that model is class i need with following:

 guard let unwrappedModel = store.state.navigationState.getRouteSpecificState(store.state.navigationState.route) as myClassOne? else {
                assertionFailure("Wrong model for RetailSalesVC")
                return
            }

Now i want to check if model is class one OR model is class two. Is that possible to achieve?

Now i ended up with this (but without optional binding):

   guard (((store.state.navigationState.getRouteSpecificState(store.state.navigationState.route) as MyClassOne?) != nil)) ||
            (((store.state.navigationState.getRouteSpecificState(store.state.navigationState.route) as MyClassTwo?) != nil)) else {
                assertionFailure("Wrong model for \(self)")
                return
        }
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • Possible duplicate of [Match the data type of a object in Swift](https://stackoverflow.com/questions/24444126/match-the-data-type-of-a-object-in-swift) – user28434'mstep Dec 06 '17 at 09:53
  • 1
    You are *wasting* too many parentheses – vadian Dec 06 '17 at 10:04
  • How would you work with `unwrappedModel ` if you know only that it is a MyClassOne *or* a MyClassTwo? – You probably want to define a common *protocol* which you can match against. – Martin R Dec 06 '17 at 10:16
  • @vadian thats look ugly i admit) – Evgeniy Kleban Dec 06 '17 at 12:54
  • @MartinR they actually do have common protocol, but that protocol shared among other classes not suitable here. So, i want to work with them like i want to work with specific protocol but i don't want other classes to pass. – Evgeniy Kleban Dec 06 '17 at 12:55
  • Then define a specific protocol for that purpose! You cannot define a variable which is of type A or of type B. How should the compiler decide which methods you can call on that variable? – Martin R Dec 06 '17 at 12:56
  • @MartinR yes i agree, probably in my case i should do it :) but in this case i will define an empty protocol and write an extensions only for purpose to check class type.. – Evgeniy Kleban Dec 06 '17 at 13:11
  • What are you going to *do* with unwrappedModel, after you have determined that it is of type A or B? – Martin R Dec 06 '17 at 13:17

3 Answers3

2

Here's an example if you want to run it:

class ModelOne {}
class ModelTwo {}

let testObjects : [Any] = [ModelOne.init(), ModelTwo.init(), NSCoder.init()]
for obj in  testObjects {
    if  (obj is ModelOne || obj is ModelTwo) {
        print("\(obj) passes")
    }else{
        print("\(obj) fails")
    }
}

should give the followig output:

main.ModelOne passes
main.ModelTwo passes 
<NSCoder: 0x7f93a0c02b40> fails
psobko
  • 1,548
  • 1
  • 14
  • 24
0
    if unwrappedModel is ClassOne{
        print("this model is of type ClassOne")
    }
    else if unwrappedModel is ClassTwo{
        print("this model is of type ClassTwo")
    }
Enea Dume
  • 3,014
  • 3
  • 21
  • 36
0

Try this

if let unwrappedModel = store.state.navigationState.getRouteSpecificState(store.state.navigationState.route){
    if unwrappedModel is MyClassOne{
        print("MyClassOne")
    }
    else if unwrappedModel is MyClassTwo{
        print("MyClassTwo")
    }
}
Sumit Jangra
  • 651
  • 5
  • 16