Showing posts with label calling methods. Show all posts
Showing posts with label calling methods. Show all posts

Sunday, May 22, 2016

Objective-C Calling Base class methods from Child class

With in any class implementation 'self' refers to the object of that class and 'super' refers to the parent class.

We can call parent class methods using the 'super' keyword.


Let's see an example in Objective-C.


     
@interface Parent : NSObject  

     - (void)thankYou:(NSString*)visitorName;

     @end

     @interface Child : NSObject  

     - (void)thankYou:(NSString*)visitorName;

     @end


Unlike in other programming languages, We don't need to give a prefix 'override' for overridden methods in Objective-C. But the only rule is subclass method should have the same name and property list of base class method for overriding.


     
@implementation Parent

          - (void)thankYou:(NSString*)visitorName{

              NSLog(@"From Parent : Thank you %@", visitorName);
          }

     @end

     @implementation Child

          - (void)thankYou:(NSString*)visitorName{

               NSLog(@"From child : Thank you %@", visitorName);
          }

     @end


In the above example, Whenever there is a visitor, Parent class says thank you when we send a 'thankYou' message to Parent class by calling thankYou method.



     
[parentObj thankYou:@"visitor1"];  // From Parent : Thank you visitor1


In my child class, I need this thankYou method but I want a thank on behalf of my child object. So I need to go for overriding this method as I can write child own logic in the method.



     
[childObj thankYou:@"visitor1"];    // From Child : Thank you visitor1


See, Same functionality but a little customization in the subclass with the help of Method Overriding.

Let's see how can we extend the functionality. I mean I need my Base class method called also first and after that my child class logic. I don't need to replace the functionality of base class. I just need to extend the functionality.


                         



In addition to completely replacing an existing implementation, you might want to extend a superclass's implementation. Here comes the super keyword. Within a method definition, super refers to the parent class of the current object. To call methods in the base class from child class, We can use this super keyword.

For example, I need both my parent and child should thank a visitor but should be properly conveyed.



     
@implementation Child

        - (void)thankYou:(NSString*)visitorName{

             [super thankYou: visitorName];  //calling baseclass method

             NSLog(@"From child : Thank you %@", visitorName);
         }

     @end


If a child object thanks, a visitor gets a thank you from both parent and child. Of course, child is saying on behalf of parent also here. 

Now If you call child object's thank you method,



  [childObj thankYou:@"visitor1"];       //  From Parent : Thank you visitor1

                                                         //  From Child : Thank you visitor1



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

Wednesday, May 18, 2016

Objective-C Calling methods based on iOS version

Whenever there is a version change, We need to update our app by taking care of deprecated things and should add the new things in the latest frameworks. For this, We need to call the methods based in iOS versions.

In this post, Let's discuss calling methods based on iOS version of the device in which our app is working. Let me take an WKWebView as example which was introduces in iOS8.

One of WkWebView's properties 'setAllowsLinkPreview' works from iOS9. If we need our WkWebView to handle web links, We need to enable it. But, We can use this only in iOS9. If we use it in lower versions, We will get the below compile time error.



     
[self.wkWebView setAllowsLinkPreview:YES];


No visible @interface for 'WKWebView' declares the selector 'setAllowsLinkPreview:'

To avoid these compile time error, We can take the help of @selector and SEL as shown below.



    
SEL selector = @selector(setAllowsLinkPreview:);

    if([self.wkWebView respondsToSelector:selector]){

        [self.wkWebView performSelector:selector withObject:[NSNumber 
                                                                                   numberWithBool:YES]];
    }


In the above code, I have taken the setAllowsLinkPreview method signature into a SEL object.
I have performed introspection to check whether my WKWebView object responds to this selector or not.

Please check my post Objective C NSObject Root Class to know what are introspection methods.

If it responds, I am performing the selector along with the parameter BOOL object. So, based on iOS version WKWebView responds to the selector.


If we perform selectors like above by checking the whether they responds to that selector or not, We can avoid compile time errors in the above example as well as run time errors also in the case of our custom classes.


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