Main.m
//
Main.m
8a10. Review of Block
//
Created by Huan on 16/2/8.
copyright©2016 year Huanxi. All rights reserved.
//
#import <Foundation/Foundation.h>
function Definition
void Test () {
}
int sum (int A, int b) {
return A + b;
}
int Main (int argc, const Char * argv[]) {
@autoreleasepool {
//Insert code here ...
// review block
// What is a block on a chunk of code
What is the difference between a//block and a function
//1. functions can only be defined outside the method
//2.block can be defined within a method or can be defined outside of a method
//3.block and functions in common: all have return values and parameters
//block definition
// no return value no parameter block
void(^testblock) () = ^{
NSLog(@ " no return value no parameter block");
};
Testblock ();
// no return value for block with parameters
void(^test2block) (int) = ^ (int a) {
NSLog(@ " you are passing in %d", a);
};
Test2block (2);
// has a return value, the block with parameters
int(^sumblock) (int,int) = ^ (int A,int b) {
return A + b;
};
int result = Sumblock (2,3);
NSLog(@ " through block calculation, the result is:%d", "result");
// with return value, blcok with no parameters
//block to redefine typedef
typedef void (^mtestblock) ();
//For Blcok with no return value, no parameters - custom
mtestblock mtestblcok = ^ () {
NSLog(@ "redefined block");
};
Mtestblcok ();
// define a special operation for Blcok
typedef void (^operationblock) ();
operationblock operationblock = ^ () {
NSLog(@ " Checking for version updates ");
};
Operationblock ();
NSLog(@ "Hello, world!" );
}
return 0;
}
Results
Review of Block