2013-12-13 2 views
2

블록을 둘러보고 사용할 수있는 방법을 찾으려고합니다. 그래서 매개 변수와 같은 블록을 블록으로 전달할 수 있는지 궁금합니다. 여기 iOS. 블록을 매개 변수로 블록 전달

은 몇 가지 예제 코드입니다 :

//declaration 
static id (^someBlock)(id) = ^(id someClass) { 
    // do some stuff to obtain class some class instance 
    // check if class instance respond to @selector 
    // if yes - perform selector 
} 

//usage 
+ (instancetype)someMethod { 
    someBlock(SomeClass.class); 
    // do additional work and return some instance type 
} 

이 잘 작동하지만 우리는 발신자가 someBlock이 완료 될 때 몇 가지 추가 물건을 수행하려는 경우 선택에 응답 발신자 의무 때문에, 충분하지 않습니다.

내 질문에 someBlock 완료 될 때 실행할 수있는 매개 변수 블록 someBlock 블록을 호출 할 수 있습니다.

//declaration 
    static id (^someBlock)(id, <b>^otherBlock</b>) = ^(id someClass, <b>????</b>) { 
     // do some stuff to obtain class some class instance 
     otherBlock(); 
    } 

어떤 조언 :

일부 같은

?

추 신 : 문제는 매개 변수로 블록을 메서드에 전달하는 것과 관련이 없습니다.

감사합니다, 베 넬린

+0

과 같이 호출은'otherBlock' 항상 같은, 아니면 당신은 어떤 블록 싶은가? 동일하면 그냥 정의 할 수 있으며 항상 someBlock의 끝에서 호출하십시오. – Macondo2Seattle

+0

고마워, 이걸 내 마음에 넣을거야, 유용하다. – bel

답변

2

이 당신이 찾고있는 무엇인가?

static id (^someBlock)(id, void (^otherBlock)()) = ^id (id someClass, void (^otherBlock)()) { 
    otherBlock(); 
    return nil; // just because you declares a `id` return type 
}; 

그리고

someBlock(someClass, ^() { 
    NSLog(@"other stuff"); 
}); 
+0

이상 정확함. 고맙습니다! – bel

+0

@VenelinSpiridonov : 환영합니다! –