Object-c: Several points to be noted in block

Source: Internet
Author: User

Excerpt from: http://www.cnblogs.com/ltpblog/p/3684127.html

Date:2015-12-4

1. Block definition

1) Description:

A. Block is a data type in OC that is widely used in iOS developmentB. ^ is a unique marker of blockC. Block implementation code is included in the {}d. In most cases, the way in which inline functions are defined and usedE. Block is somewhat similar to the C language function pointer, but it is more flexible to use2) Format:( return type) (^ block name) (parameter type) = ^ (parameter list) {code implementation}Note: If there are no parameters, the argument list after the equals sign can be deleted.  3) Example:( Void) (^myblock) (int, int) = ^ (int a, int b) {return a + B;} 2. Block issues to be aware ofQuestion one:block can use local variables declared before the definition

int i = 10;

void (^myblock) () = ^{

NSLog (@ "%d", I);

};

i = 100;

Myblock ();

the output of this section of code is ten.Note:A. When a block is defined, a copy of the contents of the current local variable is established in the block (copy)B. Subsequent changes to the value of the variable will not affect the value in the blockc. If you need to keep the value of local variables in the block, you need to use the __block keywordD. After using the __block keyword, you can also modify the value of the variable in the blockQuestion two:Blocks can use local variables prior to block, but they cannot be modified.

BOOL flag = NO;

[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {

if ([@ "Harry" Isequaltostring:obj] | | idx = = stopindex) {

*stop = YES;

flag = YES; COMPILATION Error!!!

}

}];

there is a syntax error when compiling the above code. Note:1. By default, variables outside the block are read-only in the block. 2. If you need to modify the values of the external variables, you need to add the __block keyword when declaring the variable. Question three:Passing Objects

NSString *stopname = @ "Harry";

Nsarray *array = @[@ "Zhang San", @ "John Doe", @ "Harry", @ "Zhao Liu"];

[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {

NSLog (@ "%d content is%@", (int) idx, obj);

if ([Stopname isequaltostring:obj] | | idx = = stopindex) {

*stop = YES;

}

}];

Attention:

to ensure that the code in the block is working properly, the block will automatically strongly reference the Stopname pointer when the Stopname pointer is passed to the block. Question four:Circular References

@property (nonatomic, strong) Nsmutablearray *myblocks;

#pragma mark changes the code to a method that calls self

Int (^sum) (int, int) = ^ (int x, int y) {

return [self sum:x y:y];

};

[Self.myblocks Addobject:sum];

Automatically called when the Mark object is released #pragma

-(void) dealloc

{

NSLog (@ "Demoobj is released");

}

Note:1. The result of a circular reference is that the object cannot be freed. 2. Local variables are strongly referenced by default and are freed after they leave their scope. 3. Using the __weak keyword, you can declare a local variable as a weak reference

__weak demoobj *weakself = self;

If you reference weakself in a block, the block no longer makes a strong reference to self

Int (^sum) (int, int) = ^ (int x, int y) {

return [weakself sum:x y:y];

};

Recommended:

Common errors with iOS block (i)

Common errors in iOS block (ii)--circular references

Common error in iOS block (iii)--block reference for concurrent programming

Object-c: Several points to be noted in block

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.