Objective-C Knowledge Summary (5), objective-c Knowledge Summary

Source: Internet
Author: User

Objective-C Knowledge Summary (5), objective-c Knowledge Summary

Basic use of block

1 // parameters with return values 2/* 3 Format: 4 return value type (^ variable name) (parameter type and number) = ^ (list of parameters) {6 code block statements; 7 8 return; 9 10}; 11 12 */13 // define a block14 int (^ myblock1) (int, int) with a parameter \ returned value = ^ (int x, int y) {15 16 return x + y; 17 18}; 19 20 int sum = myblock1 (10, 20); 21 NSLog (@ "sum = % d", sum ); 22 23 // assign 24 myblock1 = ^ (int x, int y) {25 26 return x * y; 27 28}; 29 30 // use the block, receiving return values 31 sum = myblock1 (10, 20); 32 NSLog (@ "sum = % d", sum); 33 34 // Parameters without return values 35 36/* 37 format: 38 void (^ variable name) (parameter type and number) = ^ (form parameter list) {39 40 Code BLOCK statement; 41 42 }; 43 44 */45 // define a variable myblock2 and assign a value of 46 void (^ myblock2) (int, int) = ^ (int a, int B) {47 48 NSLog (@ "a + B = % d", a + B); 49 50}; 51 52 myblock2 (34,12); 53 54 // define the variable first, then assign 55 myblock2 = ^ (int x, int y) {56 57 int m = x> y? X: y; 58 NSLog (@ "max = % d", m); 59 60}; 61 myblock2 (34,12 ); 62 63 // No parameter no return value block64 65/* 66 // define a block Variable without parameters \ no return value, and assign a value of 67 void (^ block variable name )() = ^ () {68 69 code block statement; 70}; 71 72 optimization: 73 void (^ block variable name) () = ^ {74 75 code block statement; 76 }; 77 78 // block Variable use 79 block variable name (); 80 81 */82 83 void (^ myBlock4 )() = ^ {84 85 NSLog (@ "xxxx"); 86 printf ("xxxxxx"); 87 88}; 89 90 // use block Variable 91 myBlock4 ();

Typedef of block

 typedef int (^myBlock)(int,int);        myBlock a = ^(int x, int y){            return x + y;        };        int c = a(1,2);       

Block access to external variables

1 int main (int argc, const char * argv []) {2 @ autoreleasepool {3 int m = 10; 4 5 NSLog (@ "1: m = % d ", m); // 10 6 NSLog (@ "2: m addr = % p", & m); // stack Area 7 // NSString * str = @ "abc "; 8 // NSLog (@ "str = % p", str); 9 10 // define the variable and assign a value of 11 // when defining the block, block copies the external variables as const 12 // store them in the memory of the block 13 void (^ myBlock )() = ^ {14 // m value cannot be modified 15 // m = 100; 16 17 NSLog (@ "5: m addr = % p", & m ); // heap 18 // The value of m can be accessed 19 NSLog (@ "3: in block m = % d", m); // 1020 21 }; 22 23 NSLog (@ "4: m addr = % p", & m); // stack zone 24 // use 25 myBlock (); 26} 27 return 0; 28}

The printed result is

// Global variables exist in the Data Segment int n = 0; int main (int argc, const char * argv []) {@ autoreleasepool {_ block int m = 10; NSLog (@ "1: m add = % p", & m); // stack address NSLog (@ "2: m = % d", m ); n = 10; NSLog (@ "7: n add = % p", & n); // data segment NSLog (@ "8: n = % d", n ); // 10 // static variable static int a = 33; NSLog (@ "---------- % p", & ); // data segment // _ block does not copy void (^ myBlock) () = ^ {int x = 100 in const mode; // stack zone // The value of m can be modified to m = 100; // global variables can be modified to n = 100; // static variables can be modified to a = 10; NSLog (@ "4: m addr = % p", & m); // heap zone // you can access the value NSLog of m (@ "3: in block m = % d ", m); // 100 NSLog (@" 9: n add = % p ", & n); // data segment NSLog (@" 10: n = % d ", n); // 100}; myBlock (); NSLog (@" 5: m = % d ", m ); // 100 NSLog (@ "6: m addr = % p", & m); // stack NSLog (@ "11: n add = % p ", & n); // data segment NSLog (@ "12: n = % d", n); // 100} return 0 ;}

The printed result is

Block usage instructions

Both static variables and global variables directly reference the variable address when the _ block parameter is added. This means that you can change the variable value without adding the _ block parameter.

Global block: The block defined outside the function is global (all). If the block inside the function does not capture any automatic variables, it is also global.

Stack block: The difference is whether external variables are referenced.

Heap block: copy the stack block. It does not have any effect on global block copy, and the returned global block is still

Block as the return value of the Function

// Defines a new type newType2typedef int (^ myBlock) (int, int); myBlock test () {// return block type return ^ (int a, int B) {return a + B ;};}int main () {myBlock n = test (); int a = n (1, 2); NSLog (@ "a = % d ", a); // 3}

Inlineblock

Block can also be defined in this way.

// You can add the form parameter int (^ block) (int x, int y) = ^ (int x, int y) {return x + y ;};

Protocol

What is a protocol?

Some method declarations are generally written to a. h header file.

There are two methods: 1) must be implemented; 2) Select implementation

Role of the Protocol:

For other classes to comply with. If a class complies with a protocol, it should implement the methods that must be implemented as defined in this Protocol.

Protocol writing

@ Protocol xxxx <NSObject> // required method (default) @ required // optional method @ optional @ end

Steps to comply with the Protocol: 1) import the header file 2) comply with the Protocol 3) Implementation Method

Protocol type restrictions

Type 1 restriction: Add a restriction to the id type

Id <xxxxxxx> obj;

After <xxxxxxx> is added, it indicates that obj can only assign values to objects that comply with the xxxxxxx protocol.

Id <xxxxxxx> obj = d;

Type 2 restrictions:

Indicates that when obj2 is assigned a value, it must be a Girl object and comply with the xxxxxxx protocol.

Girl * mm = [Girl new];

Girl <xxxxxxx> * obj2 = mm;

Protocol proxy Design Mode

Please refer to my previous blog> --- click here --- <

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------

The other four chain stores are as follows:

Objective-C Knowledge Summary (1)

Objective-C Knowledge Summary (2)

Objective-C Knowledge Summary (3)

Objective-C Knowledge Summary (4)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.