Block as a completion Handler in iOS


Block as a completion Handler in iOS

In Objective-c

declare a méthode::

-(void) getCallBack:(NSString *)string1 onCompletion:(void(^)(NSString *sting))handeler;

function implementation:

-(void) getCallBack:(NSString *)string1 onCompletion:(void(^)(NSString *sting))handeler{

    

    handeler(string1);

}

function calling:

[self  getCallBack:@”Sunny” onCompletion:^(NSString *value){

        NSLog(@”My name is %@”,value);

    }];

In Swift

function declaration::

func getValueByMultiPle(value1:Int,value2:Int,onCompletion:((success:Int)->Void),onError:((errorValue:String)->Void)){

        

        if(value1>0 && value2>0){

            onCompletion(success: value1*value2)

        }else{

            onError(errorValue:“Error”)

        }

    

    }

function calling ::

User.shareInstance.getValueByMultiPle(0, value2:10, onCompletion: { success in

                println(success)

            }, onError: { error in

                println(error)

        })

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: