Ios development-What block should know

Source: Internet
Author: User

Block is now widely used, and there are also many pitfalls in use. Below we will summarize the several things that should be known in block development.: inline (inline) Block inline block to put it into a function. For example, typedef void (^ HelloBlock) (void);-(void) blockTest {HelloBlock myBlock = ^ (void) {NSLog (@ "Hello Block") ;}; myBlock () ;}note 1. internal variables defined in the inline block have read and write permissions. to modify the external variables inside the block, you must declare the external variables _ block 3. the Block retain is invalid. to retain the block lifecycle, you can use copy to implement it. Remember release 4. the retainCount of the application to be block will automatically add one. To break this retain circle, you can add _ block before the object, in this way, the block will not maintain this object. If _ block a is not added, the following statement cannot dealloc _ block A a = [[[A alloc] init] withBlock: ^ {[a action]; [a release] ;}]; // The dealloc method of a will call @ interface ViewController: UIViewViewController {NSString * _ string ;} _ block ViewController * controller = self; _ block = ^ {NSLog (@ "string % @", controller-> _ string) ;}; 5. self cannot be referenced in an independent block. If you need to access it, you can use the parameter transfer method (you can consider it as a copy of the input function pointer in c +) ** 6. do not use it unless necessary. syntax # import <UIKit/UIKit. h> @ interface AppDelegate: NSObject <UIApplicationDelegate> @ property (nonatomic, strong) NSString * stringProperty; @ end # import "GCDAppDelegate. h "@ implementation AppDelegate @ synthesize stringProperty;-(void) BlockTestError {HelloBlock myBlock = ^ (void) {self. stringProperty = @ "Block Objects"; NSLog (@ "String property = % @", self. stringProperty); // running error}; myBlock () ;}- (void) BlockTestCorrect {HelloBlock myBlock = ^ (void) {[self setStringProperty: @ "Block Objects"]; NSLog (@ "self. stringProperty = % @ ", [self stringProperty]); // run OK}; myBlock () ;}@ end in addition, I intend to resign for a job after 11, which of the following is the company's intention to contact the insite email to help you push it down. the personal blog I set up has also been launched to reference hexo. The address is hufeng825.github.com and the ui color is being adjusted, we recommend that you use chrome Firefox or safrari to browse ie10 or below. In addition, you have responded to the layout of mobile devices.

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.