为了避免保留周期和警告“在此块强烈捕捉自我很可能会导致保留周期”我在我的块中添加一些为每个对象这样无__block变量设置一个NSString的
MyViewController *__weak weakSelf= self;
NSMutableArray *__weak weakArray=AnArray;
UILabel *__weak weakLabel=ALabel;
///the block code with some examples
up2.completionBlock = ^(NSDictionary *headers, NSString *responseString) {
[weakSelf aMethodInTheController];
[weakLabel setHidden:NO];
[weakArray addObject:@"something"];
};
与弱引用保留周期警告消失了,但是有,如果我的块中引入了必须设置好的
MyViewController *__weak weakSelf= self;
NSMutableArray *__weak weakArray=AnArray;
UILabel *__weak weakLabel=ALabel;
NSString *__weak weakString=AString;
///the block code with some examples
up2.completionBlock = ^(NSDictionary *headers, NSString *responseString) {
[weakSelf aMethodInTheController];
[weakLabel setHidden:NO];
[weakArray addObject:@"something"];
[email protected]"atext";
};
这样,我又收到错误nnstring问题“变量不分配(失踪_ 块类型指定IER)”,所以我必须添加‘ _block’
MyViewController *__weak weakSelf= self;
NSMutableArray *__weak weakArray=AnArray;
UILabel *__weak weakLabel=ALabel;
NSString *__weak weakString=AString;
__block NSString *BlockString = weakString;
///the block code with some examples
up2.completionBlock = ^(NSDictionary *headers, NSString *responseString) {
[weakSelf aMethodInTheController];
[weakLabel setHidden:NO];
[weakArray addObject:@"something"];
[email protected]"atext";
};
这样一切似乎正常工作,至少要等到我是块里面......我原来ASTRING变量在.H定义我的viewController,我不得不访问它的价值之后和块外,但它的价值永远是零。将@“atext”分配给BlockString不要为我的原始Astring变量设置一个值?我怎么能设置一个变量的值设置在我的viewController的.h没有收到警告和没有退出块?