Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Block variable in objective C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 225
    Comment on it

    Block access the data in normal way as the other normal function do. Block can access any class variable or function variable out side it but cannot modified it.

    int x= 111;
    
    void (^printXAndY)(int) = ^(int y) {
        printf("%d %d\n", x, y);
    };
    printXAndY(333);
    

    output will be :

    111 333
    

    But if you try to assing the new value to variable x. then it will not change the value. For example

    int x= 111;
    void (^printXAndY)(int) = ^(int y) {
         x=x+y
        printf("%d %d\n", x, y);         //  error here
    };
    

    so if you want change the value then you can user __block before the variable. for example

    _block int x=111;
    

    then it upper block will the expected result. And it will add the both variable.

    111+333=444.
    

 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: