: 그래서 하나의 옵션 왼쪽이 블록 (비동기 작업 기간 동안 만 의미가 있음)이므로 최종 릴리스에서는 사용한 후에 사용해야합니다. 또는 간단히 다음을 수행 할 수 있습니다.
NSMutableArray *objArray = [NSMutableArray array];
그리고이 경우에는 해제 할 필요가 없습니다.
동기화 전화 인 경우 차단 후 release
해야합니다.
참고 : 나는 당신이 블록이 시작되기 전에 생성 할 말이 의미 블록에 사용되기 전에 NSMutableArray
을 채우기되어 가정입니다.
비동기 방식 :
-(void) doSomething
{
// Remove the `__block` qualifier, you want the block to `retain` it so it
// can live after the `doSomething` method is destroyed
NSMutableArray *objArray = // created with something useful
[self performOperationWithBlock:^(void)
{
// You do something with the objArray, like adding new stuff to it (you are modyfing it).
// Since you have the __block qualifier (in non-ARC it has a different meaning, than in ARC)
// Finally, you need to be a good citizen and release it.
}];
// By the the time reaches this point, the block might haven been, or not executed (it's an async call).
// With this in mind, you cannot just release the array. So you release it inside the block
// when the work is done
}
동기화 접근는 :
이 블록 후, 즉시 결과를해야하고, 당신이 배열에 추가 작업을 할 때 의미가 있다고 가정 실행되었습니다 :
-(void) doSomething
{
// Keep `__block` keyword, you don't want the block to `retain` as you
// will release it after
__block NSMutableArray *objArray = // created with something useful
[self performOperationWithBlock:^(void)
{
// You do something with the objArray, like adding new stuff to it (you are modyfing it).
}];
// Since it's a sync call, when you reach this point, the block has been executed and you are sure
// that at least you won't be doing anything else inside the block with Array, so it's safe to release it
// Do something else with the array
// Finally release it:
[objArray release];
}
'__block'은 블록에 rray, 그래서 버전 2는 더 논리적이다. 나는 건설에'autorelease'를 사용할 것입니다. –
블록의 배열에 개체를 추가하려는 경우에는 '__block'이 필요하지 않습니다. –