반응형

Objective C --  문법정리 : block



참고 : http://stackoverflow.com/a/9201774

         http://rypress.com/tutorials/objective-c/blocks

         http://www.tutorialspoint.com/objective_c/objective_c_blocks.htm




Block  == 다른언어의 무명함수, 람다함수




1. Blocks as Variables



return_type (^blockName)(var_type) = ^return_type (var_type varName)

{

    // ...

};


또는 


return_type (^blockName)(var_type) = ^(var_type varName)

{

    // ...

};



주석 ;  ^return_type (var_type varName) 에서 return_type생략가능 !


예 1.

void (^simpleBlock)(void) = ^{

    NSLog(@"This is a block");

};

simpleBlock();


예 2.

double (^multiplyTwoValues)(double, double)

    ^(double firstValue, double secondValue) {

   return firstValue * secondValue;

    };

double result = multiplyTwoValues(2,4);




2. typedef Block


typedef return_type (^blockName)(var_type);



예 .

#import <Foundation/Foundation.h>


typedef void (^CompletionBlock)();

@interface SampleClass:NSObject

- (void)performActionWithCompletion:(CompletionBlock)completionBlock;

@end


@implementation SampleClass


- (void)performActionWithCompletion:(CompletionBlock)completionBlock{


    NSLog(@"Action Performed");

    completionBlock();   // 매개변수로 받은 Block 함수를 실행.

}


@end


int main()

{

    /* my first program in Objective-C */

    SampleClass *sampleClass = [[SampleClass alloc]init];

    [sampleClass performActionWithCompletion:^{

        NSLog(@"Completion is called to intimate action is performed.");

    }];   // 매개변수로 Block 함수 넘김.

    

    return 0;

}



3. Blocks as Properties


@property (copy) return_type (^blockName) (var_type);





4. Blocks as Arguments


[someObject doSomethingWithBlock: ^return_type (var_type varName)

{

    //...

}];




5. method parameter


- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;



예 .

// Car.h

#import <Foundation/Foundation.h>


@interface Car : NSObject


@property double odometer;


- (void)driveForDuration:(double)duration

       withVariableSpeed:(double (^)(double time))speedFunction

                   steps:(int)numSteps;


@end


// Car.m

#import "Car.h"


@implementation Car


@synthesize odometer = _odometer;


- (void)driveForDuration:(double)duration

       withVariableSpeed:(double (^)(double time))speedFunction

                   steps:(int)numSteps {

    double dt = duration / numSteps;

    for (int i=1; i<=numSteps; i++) {

        _odometer += speedFunction(i*dt) * dt;

    }

}


@end



// main.m

#import <Foundation/Foundation.h>

#import "Car.h"


int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Car *theCar = [[Car alloc] init];

        

        // Drive for awhile with constant speed of 5.0 m/s

        [theCar driveForDuration:10.0

               withVariableSpeed:^(double time) {

                           return 5.0;

                       } steps:100];  // 매개변수로 Block 함수 넘김.

        NSLog(@"The car has now driven %.2f meters", theCar.odometer);

        

        // Start accelerating at a rate of 1.0 m/s^2

        [theCar driveForDuration:10.0

               withVariableSpeed:^(double time) {

                           return time + 5.0;

                       } steps:100];  // 매개변수로 Block 함수 넘김.

        NSLog(@"The car has now driven %.2f meters", theCar.odometer);

    }

    return 0;

}



6. Anonymous Block


^return_type (var_type varName)

{

    //...

};




7. Inline Block


^return_type (var_type varName)

{

    //...

}(var);




8. Recursive Blocks


__block return_type (^blockName)(var_type) = [^return_type (var_type varName)

{

    if (returnCondition)

    {

        blockName = nil;

        return;

    }


    // ...

} copy];


blockName(varValue);




9. Returning Blocks


- (return_type(^)(var_type))methodName

{

    // ...

}

as can a function, if a bit strangely.


return_type (^FunctionName())(var_type)

{

    // ...

}
















반응형
Posted by 자유프로그램
,