Block series 2: block memory management

Source: Internet
Author: User

Viewcontroller. h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    UIImage *_image;    NSInteger _index;}@end
Viewcontroller. m
# Import "viewcontroller. H "# import" person. H "@ interface viewcontroller () @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; // Rule 1 -- block calls the object-C, the object will be modified by retain uibutton * button = [[uibutton alloc] init]; // retaincount 1 // using _ block, the reference count does not add 1 _ block uibutton * btn2 = [[uibutton alloc] init]; // retaincount 1 // rule 2 -- the basic data type of block calls, the basic data type is regarded as constant int number = 10; _ block int num = 20; // rule 3 -- block references instance variables (global variables or attributes can be considered ), the object of this instance will be retain _ image = [[uiimage alloc] init]; _ Index = 30; // declare and add void (^ myblock) (INT) = ^ (int A) {// object nslog (@ "button address: % P", button); // retaincount 2 nslog (@ "btn2 address: % P ", btn2); // retaincount 1 // basic data type // error, constant cannot be assigned again // number = 20; num = 10; nslog (@ "Num: % d", num + a); // This method is not accurate. (required) // do not use this method. (required) // nslog (@ "% d", button. retaincount); // instance variable [attribute] // block references image. The object of Image Self [viewcontroller] will be retain nslog (@ "_ image address: % P ", _ image) ;}; myblock (10); [button release]; [btn2 release]; person * person = [[person alloc] init]; // implement block [person testblock: ^ (int A) {// _ index is a global variable // block reference _ index, the self [viewcontroller] object to which _ index belongs will be retain nslog (@ "A + _ Index = % d", A + _ index) ;}];}
Person. h

# Import <Foundation/Foundation. h> typedef void (^ personblock) (int A); @ interface person: nsobject // declare block-(void) testblock :( personblock) block; @ end
Person. m

# Import "person. H" @ implementation person // call block-(void) testblock :( personblock) block {block (10 );}





Block series 2: block memory management

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.