Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Blocks in Objective-C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 213
    Comment on it

                                                                                          Blocks in Objective-C

     

    Blocks can be define in simple words as functions or methods which can be pass as value to other functions or methods. Block itself is a method which includes some lines of code that can be access by user when ever required. I will explain the concept of blocks in Objective-C language with the help of an example which is given below:-

    For creating block example in Xcode we need to add a class first of all.Suppose we add a class named Blockexample. After adding the class two parts of a class are available one is interface where declaration of methods will be done and another is implementation part where definition of methods will be done but as we are working on blocks so the concept is slightly different here. Defination of created block will be done in main method.Now I am going to declare one method in interface part of a class i.e BLockexample.h with the help of .h extension it is clear that this part includes interface.

     

    Blockexample.h

    #import <Foundation/Foundation.h>
    
    typedef void(^addblock)(int,float);  //declaration of block with parameter
    @interface blockexample : NSObject
    -(void)mul:(addblock)firstmethod andb:(addblock)secondmethod; //declaration of method
    @end

     After the declaration of block and method now I will tell you what we are going to do in implementation part of a class.Implementation part of a class can be identify with .m extension.So the class which we have used include Blockexample.m implementation part.

     

    Blockexample.m

    #import "blockexample.h"
    
    @implementation blockexample
    -(void)mul:(addblock)firstmethod andb:(addblock)secondmethod
    {
        firstmethod(1,6.2);        //method a calling
        secondmethod(8,3.4);        //method b calling
    }
    @end


    In this part we have called the two method which we have declared in interface part with return type as addblock.Now in main method I will define the above two methods which is given below:-

     

    main.m

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    #import "blockexample.h"
    
    int main(int argc, char * argv[])
    
    {
        @autoreleasepool {
            blockexample *bobj=[[blockexample alloc]init];
            [bobj mul:^(int a, float b) {
                int a1=a,b1=b;
                float c;
                c=a1*b1;
                NSLog(@"Multiplication is %f",c);
            } andb:^(int a, float b) {
                int a1=a,b1=b;
                float c;
                c=a1*b1;
                NSLog(@"Multiplication is %f",c);
            }];
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }

    Now after all this programming part I will debug the program to see the output.Output will be shown in this way as below:-

    Output

    2016-02-24 14:13:21.528 blockexample24[2694:83887] Multiplication is 6.000000
    2016-02-24 14:13:21.528 blockexample24[2694:83887] Multiplication is 24.000000

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: