【发布时间】:2012-10-23 01:48:54
【问题描述】:
我对在块中使用 self 感到困惑,我浏览了一些 Apple 的文档,但仍然找不到正确的答案。
有些人总是说在块中使用弱自我,但有些人说在复制的块中使用弱自我,而不是总是使用。
样本 1:
self.handler = ^(id response, NSError *error)
{
self.newresponse = response; //use weak self here
};
示例 2:
利用弱自我;
__weak myViewController *weakSelf = self;
[UIView animateWithDuration:interval delay:0.0 options:curve animations:^
{
[weakSelf.view.superview setTransform:CGAffineTransformMakeTranslation(0, -106)];
//in above is it use of weak is neassary
}
completion:^(BOOL finished)
{
}];
没有弱的自我;
__weak myViewController *weakSelf = self;
[UIView animateWithDuration:interval delay:0.0 options:curve animations:^
{
[myViewController.view.superview setTransform:CGAffineTransformMakeTranslation(0, -106)];
}
completion:^(BOOL finished)
{
}];
在上述示例中,哪些是正确的……? **我正在使用 ARC
【问题讨论】:
标签: cocoa-touch cocoa objective-c-blocks