Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Passing block as function argument in Objective C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 550
    Comment on it

    Yes, we can pass the Block as argument in function. Here it is :

    let suppose i create a class Downloader

    First declare a Block in Downloader.h:

    typedef void (^DownloadComplete)(double,double,double,BOOL,id,id);
    

    then declare method as following in Downloader.m:

    -(void)startdownload:(id)Object andCompletion:(DownloadComplete)completion;
    

    And here it is implementation :

    -(void)startdownload:(id)Object andCompletion:(DownloadComplete)completion{
    // here will come you logic and other formation after that simply call the block
        completion(0,0,0,YES,response,Object);
    }
    

    And in this way we can call that method in which Block as function argument:

    Downloader *downloader=[[Downloader alloc] init];
    [downloader startdownload:self andCompletion:^(double bytesread, double totalBytesRead, double totalBytesExpectedToRead, BOOL flag, id object, id object) {
    
    // Here you can handle the response and do what you want to do with it.
    
    }
    

 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: