爲了避免保留週期和警告「在此塊強烈捕捉自我很可能會導致保留週期」我在我的塊中添加一些爲每個對象這樣無__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沒有收到警告和沒有退出塊?