Im new to the block programming in ios, Ive read many guides and they say, things get retained in a block, and I write a demo to test the retain cycle they mentioned.
header file:
typedef NSString* (^MyBlock)(void);
@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
{
UIView * testView;
SubDetailViewController * tSubDetailViewController;
NSMutableArray * array;
MyBlock block1;
}
m file: in viewDidLoad:
array = [[NSMutableArray alloc] init];
block1 = ^(void){
[array addObject:@"23"];
[self btn2Touch:nil];
return @"3";
};
NSLog(@"self after block retainCount -> %d",self.retainCount);
NSLog(@"array after block retainCount -> %d",array.retainCount);
//self.block1();
[array release];
I thought the array and self will be retained, retatinCount +1; but whether I do self.block1(), or not, the retainCount not +1, everything seems fine, array could be released,when pop the view controller, self release normally.
do I miss something with guides? so curious abt this situation. anyone could give me a code of retain cycle with block?
arrayis not captured by the block. Onlyselfis.arrayis an instance variable, so anywherearrayappears by itself, it is implicitlyself->array. Only local variables can be "captured". - newacct