0

I have an array of viewController class names like (ViewController class 1, 2 ,3 4)

 let classArray = [VCclass1, VCclass2, VCclass3, VCclass4]

I want to check the object belongs to any of the class mentioned in class array like

if obj.isKind(of:(Any of the classArray elements) ) {

 //do something

 } else {

//execute else condition

}

How can I write "if condition" here?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ashh
  • 569
  • 1
  • 6
  • 28

3 Answers3

2

Well, you're looking to see if your array contains a member according to the type of obj. So, contains(where:) is a perfect fit:

if classes.contains(where: { type(of: obj) == $0 }) {
    //do something
} else {
    //execute else condition
}
Alexander
  • 59,041
  • 12
  • 98
  • 151
  • yes, this is a much cleaner way than my answer. very nice...not to mention it also supports non-NSObject classes. – Jake Feb 14 '18 at 03:51
  • @Jake Whenever you write a for loop like yours, just consider: "Can I use `map`, `filter`, `reduce`, `contains(where:)`, or `index(where:)`?". More often than not, you can – Alexander Feb 14 '18 at 14:09
  • the question is not about searching in the array, the question is about how to find the obj is kind of any of the classes in array. @Shial solution would works best I guess – Maryam Fekri May 30 '18 at 15:38
  • @MaryamFekri Apart from him using the ObjC API (`isKind(of:)`) and me using the Swift variant (`is`), our answers are the same ... – Alexander May 30 '18 at 16:26
  • you code doesn't work for me! I have array of UIView.Type but it says $0 expected to be Type – Maryam Fekri May 30 '18 at 16:50
  • oops, should be `type(of: obj) == $0` https://repl.it/repls/ComfortableCloudyShockwave – Alexander May 30 '18 at 17:34
2

You can verify that in a simple way.

import Foundation
import UIKit

class A: UIViewController {}
class B: UIViewController {}
class C: UIViewController {}

let array:[UIViewController.Type] = [A.self,B.self,C.self]
let obj = A()

print(array.contains(where: { obj.isKind(of: $0) }))

Output should be true. You can run this code in Playground However I would recommend switch for that purpose. In more complicated scenario you will want to know which class is given object or so.

switch obj {
case is A:
    print("A")
case is B:
    print("B")
case is C:
    print("C")
default:
    print("none")
}
Shial
  • 1,386
  • 19
  • 31
0

Try this:

extension NSObject {
    func isKind(of classes: [AnyClass]) -> Bool {
        for aClass in classes {
            if self.isKind(of: aClass) {
                return true
            }
        }
        return false
    }
}

let classes = [UIViewController.self]
let obj = UIViewController()

if (obj.isKind(of: classes)) {
    //do something
}

or the uglier, less OOP way...

var isKindOfClass = false
for aClass in classes {
    if obj.isKind(of: aClass) {
        isKindOfClass = true
        break
    }
}

if isKindOfClass {
    //do something
}
Jake
  • 13,097
  • 9
  • 44
  • 73
  • 1
    Good answer, but there's no need to reinvent the wheel. Just use `contains`. Furthermore, don't use `isKind(of:)` in Swift. Just use `is`. https://stackoverflow.com/a/40388434/3141234 – Alexander Feb 14 '18 at 03:48