Friday, January 27, 2017

Swift 3.0 Object’s class checking isKindOfClass

We frequently need this type checking when getting response from the server. Where we need to check the type of objects based on which we need to parse the response.

In this post let’s see how can we check this using Swift 3.0.

In Objective-C/ Swift, It’s NSObject’s method isKindOfClass which helps for type checking.

I have two types of classes.

Employee



  
    var firstName: String?

    var lastName: String?
    var email: String?
    var gender: String?
    var isMarried: Bool = false
    

    init(firstName: String, lastName: String, email: String, gender: String, 
                                                                              isMarried: Bool) {
        self.firstName = firstName
        self.lastName = lastName
        self.email = email
        self.gender = gender
        self.isMarried = isMarried
    }


Employer



  
    var firstName: String?

    var lastName: String?
    var email: String?
    var gender: String?
    var isMarried: Bool = false
    var employeesCount:Int = 0    

    init(firstName: String, lastName: String, email: String, gender: String, 
                                                    isMarried: Bool, employeesCount:Int) 
    {
        self.firstName = firstName
        self.lastName = lastName
        self.email = email
        self.gender = gender
        self.isMarried = isMarried
        self.employeesCount = employeesCount
    }


Only Employer will have employees count.

Let’s take an array of Employee and Employer objects.


  
           let employee1 = Employee.init(firstName: "employee-1", 
                                                          lastName: "lastname-1", 
                                                          email: "employee1@gmail.com", 
                                                          gender: "Male", 
                                                          isMarried: true)

           let employee2 = Employee.init(firstName: "employee-2", 
                                                          lastName: "lastname-2", 
                                                          email: "employee2@gmail.com", 
                                                          gender: "Male", 
                                                          isMarried: true)

           let employer1 =  Employer.init(firstName: "XXX", 
                                                          lastName: "YYY", 
                                                          email: "xyz@gmail.com", 
                                                          gender: "Male", 
                                                          isMarried: true,
                                                          employeesCount: 32)
        
        
           let employees = [employee1, employee2, employer1]



I have taken 2 Employee objects and 1 Employer object and added them to an array. This is kind of Any Array in Swift.

Now the task is, We need to loop through this array and print employee count if the object is an Employer. Let’s see how this can be using Swift 3.0


  
        let employees = [employee1, employee2, employer1]

        
        for employee in employees {
            
            if (employee.isKind(of: Employer.self)) {
                
                let employeesCount = (employee as! Employer).employeesCount
                
                print("employeesCount : \(employeesCount)")   // employeesCount : 32
                
            }
            
        }



Hope this post is useful. Feel free to comment incase of any queries.



1 comment:


  1. This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating these kinds of informative things ios app development course

    ReplyDelete