1. Hello, world
#import<Foundation/Foundation.h>
int Main ()
{
/*my First program in OBJECTIVE-C*/
NSLog (@"Hello, world! \ n");
return0;
}
2. Block (function pointer like C, JS closure, c++11 lambda functions)
(1) Example
__blockintsum =0;
int(^MYBLOCKS3) (intAintb) = ^ (intAintb) {
sum = a+b;
return3;
};
MYBLOCKS3 ( -, -);
NSLog (@ "sum is%d", sum);
(2) Example
#import<Foundation/Foundation.h>
//define a typedef for blocks
typedefint(^sumblockt) (intAintb);
intMain (intargcConstChar* argv[])
{
@autoreleasepool {
//Insert code here ...
NSLog (@"Hello, world!.");
void(^myblocks) (void) = NULL;
Myblocks = ^ (void) {
NSLog (@"In blocks");
};//assigning values to Myblocks
NSLog (@"before Myblocks");
Myblocks ();//implementation of Myblocks;
NSLog (@"After myblocks");
/*
Before Myblocks
In blocks
After Myblocks
*/
int(^MYBLOCKS2) (intAintb) = ^ (intAintb) {
intc = a+b;
returnC
};
NSLog (@"before Blocks2");
intret = MYBLOCKS2 (Ten, -);
NSLog (@"After BLOCKS2 ret%d", ret);
__blockintsum =0;
int(^MYBLOCKS3) (intAintb) = ^ (intAintb) {
sum = a+b;
return 3;
};
MYBLOCKS3 ( -, -);
NSLog (@"sum is%d", sum);
Sumblockt MYBLOCKS4 = ^ (intAintb) {
NSLog (@"C is%d", a+b);
return0;
};
MYBLOCKS4 ( -, -);
}
return0;
}
3. Grammatical sugars
OBJECTIVE-C Study Notes