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