Cha*_*all 6 objective-c objective-c-blocks
我已经开始使用块了,我遇到的第一件事就是无法设置闭包捕获的值.这很好,我一直在使用C/C++很长一段时间.我只会使用指针!
MyObject* bestObj = nil;
float bestDist= 10000.f;
MyObject **pBestObj = &bestObj;
float* pBestDist = &bestDist;
[self testObjects:class block:^(MyObject* obj){
CGRect r = [obj boundingBox];
// position is captured from outside this code sample
if( CGRectContainsPoint( r, position ) )
{
float dist = GetDistance( obj, position );
if(dist < bestDist)
{
*pBestDist = dist;
*pBestObj = obj;
}
}
}];
return bestObj;
Run Code Online (Sandbox Code Playgroud)
我的问题是,这样安全吗?我假设只要我的指针指向一些没有超出范围但仍然存在的东西,它应该可以工作.但是我也假设那些采用块的东西并不是说并行运行它们.我知道我的代码没有,但我不知道,比如使用带有NSArray enumerateObjectsUsingBlock调用的块.
| 归档时间: |
|
| 查看次数: |
125 次 |
| 最近记录: |