Showing posts with label isMemberOfClass. Show all posts
Showing posts with label isMemberOfClass. Show all posts

Sunday, June 26, 2016

Objective C Checking JSON format

In iOS applications, We use JSON format for web services as it is fast and easy to parse. JSON could be either in array or in dictionary format. It is a good practice to JSON format before parsing the response from a web service though we have perfect handshake with web service developers.


Let's take an example of fetching all the employees using a web service and there is a chance of JSON coming in any format as shown below.

JSON Dictionary:


 {"employees":

   [

     {"firstName":"Tim""lastName":"Cook"},

     {"firstName":"Will""lastName":"Smith"},

     {"firstName":"Leonardo""lastName":"Dicaprio"}

   ]


 }



JSON Array:


 [

    {"firstName":"Tim""lastName":"Cook"},

    {"firstName":"Anna""lastName":"Smith"},

    {"firstName":"Peter""lastName":"Jones"}


 ]



Using introspection methods in Objective C, We can check the type of JSON.


                         




  if([response isKindOfClass:[NSArray class]]){

      //JSON is in Array Format
  }

  if([response isKindOfClass:[NSDictionary class]]){

      //JSON is in Dictionary Format

  }




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




Wednesday, September 16, 2015

Objective C NSObject Root Class

All the Objective C programmers know that the root class of all Objective C classes is NSObject. It is part of NSFoundation framework. Root class inherits from no other class. Let's see why it a root class and what it gives us to maintain that big name called 'Root Class'.

Here I am just detailing the information given in the Apple Documentation. I feel like the information is briefed in that documentation. I Just want to discuss in detail on this topic in this article. So If any one finds it similar to that apple documentation, please don't come back here and call me a copy cat :)

1. NSObject declares the fundamental object interface and 
    implements basic object behaviour.

2. NSObject gives us Introspection methods :

  • Methods like isKindOfClass and isMemberOfClass, to determine if an object inherits directly or indirectly from a particular class. For more information on these methods, checkout out my article here.
  • Methods like respondsToSelector, to find out if an object's class or super class implements a method.
  • Methods like confirmsToProtocol, to find out if a class confirms a particular protocol or not.


      So from this, and also as per the Apple Documentation,


  • You can call introspection methods as runtime checks to help you avoid problems such as exceptions, which, for example, would occur if you send a message to an object that cannot respond to it.
  • You can also use introspection to help locate an object in the inheritance hierarchy, which would give you information about the object’s capabilities.
3. NSObject gives us Memory management methods :

     Methods like alloc, dealloc, retain, copy etc.

4. NSObject gives us Method Invocation methods.



As it is giving us all these, we call NSObject as the root class of all Objective C classes.

Tuesday, September 15, 2015

Objective C isKindOfClass VS isMemberOfClass

We all know that NSObject is the base class in Objective c. Some times, We need to inherit the built in classes like NSArray, NSString and NSDictionary etc to add our required functionalities which are not given by those core classes. Like, I want an array which should give me all the elements by removing the duplicates if any. We can go for NSSet also for this feature. But, I need NSArray for a specific reason. In this case we need to write our own class which inherits from NSArray class. Suppose if I need that same functionality on NSString also, I need to create another sub class of NSString class.


                         



Instead of creating two separate sub classes for that similar functionality, We can write a method which accepts any data type as input, removes duplicates and returns the same data type as the return value.

Here is the method I am using to remove duplicates in NSArray or NSString.


- (id)removeDuplicates:(id)input{
    
    id output = nil;
    
    if([input isKindOfClass:[NSArray class]]){
        //loop array elements and remove duplicates and return an array
    }
    
    if([input isKindOfClass:[NSString class]]){
        //loop string characters and remove duplicates and return a string
    }
    
    return output;
    
}

If you observe, the input is of type id and output is of type id, which means any data type in Objective c. Here the point is how we can check whether the input is of type array or a string. In this case we can use NSObject's isKindOfClass to check whether the supplied input is of type NSArray or NSString. Based on the data type of the input, we can write that removing duplicates logic and return the result.

Now, let's see what is the use of NSObject's isMemberOfClass.

Let's say we have a Animal class. We have two sub classes of Animal class called Lion and Dog. I am not going to explain how to write such a classes as we are all much aware of that much object oriented programming using Objective c :)

 Lion *lion = [Lion alloc] init];  // Base class is Animal
 Dog *dog  = [Dog alloc] init];   // Base class is Animal


Let's say these animals have to make sound if they see their enemy. We all know that dogs bark and lions roar :). Let's say we have common method for that called 'makeSound'. 

- (id)makeSound:(id)input{    
      
    if([input isKindOfClass:[Lion class]]){
        [input roar];
    }    
    if([input isKindOfClass:[Dog class]]){
        [input bark];
    }      
}


In this case, like shown in the above code, if you use isKindOfClass, our dog is never gonna bark and our lion is never gonna roar :). It is because, isKindOfClass doesn't give you 'true' bool value for Lion and Dog classes. It will give you true value only if you write like the below code. But this always gives you roar sound though it's a dog or a lion.


- (id)makeSound:(id)input{    
      
    if([input isKindOfClass:[Animal class]]){
        [input roar];
    }    
    if([input isKindOfClass:[Animal class]]){
        [input bark];
    }      
}


Here comes isMemberOfClass, which gives true for immediate base class as shown in the below code.

- (id)makeSound:(id)input{    
      
    if([input isMemberOfClass:[Lion class]]){
        [input roar];
    }    
    if([input isMemberOfClass:[Dog class]]){
        [input bark];
    }      
}


Hope, you got an idea on the use cases of both isKindOfClass and isMemberOfClass and the difference between those two.



                         


Feel free to comment in case of any queries/concerns.