2014-10-09 2 views
3

신속하게 메소드를 호출하려고합니다.swift에서 objective-C typedef 블록 호출

typedef void(^VPersonResultBlock)(Person *person, NSError *error); 

- (void)askForMe:(VPersonResultBlock)block; 

여기에서 해당 메소드의 구현이다 : 있어서

헤더 파일 블록 대물-C 작성된 싱글이다.

- (void)askForMe:(VPersonResultBlock)block 
{ 
if (_me) block(_me,nil); 
else { 
    [Person getMeWithBlock:^(PFObject *person, NSError *error) { 
     if (!error) { 
      _me = (Person *)person; 
      block(_me,nil); 
     } 

     else if (error) { 
      block(nil,error); 
     } 
     else { 
      NSDictionary *userInfo = @{ 
             NSLocalizedDescriptionKey: NSLocalizedString(@"Operation was unsuccessful.", nil), 
             NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The operation failed to retrieve the user.", nil), 
             NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString(@"Check your network connection and try again", nil) 
             }; 
      NSError *error = [[NSError alloc] initWithDomain:@"VisesAsyncErrorDomain" code:-10 userInfo:userInfo]; 
      block(nil,error); 
     } 
    }]; 
} 
} 

Objective-C에서는 이것을 혼동없이 호출 할 수 있습니다.

[[VDataStore instance] askForMe:^(Person *person, NSError *error) { 
    // do things with myself that aren't strange 
}]; 

이제는 동일한 방법을 신속하게 호출한다고 가정 해 보겠습니다. 헤더 파일을 가져 와서 브리징 헤더가 설정되지만 신속한 예상은 혼란 스럽습니다.

VDataStore.askForMe(VDataStore) 

이, 내가 바라던 자동 완성 옵션

(VPersonResultBlock!) -> Void askForMe(self: VDataStore) 

에 표시 무엇이 폐쇄에 자동 완성을 위해, 그리고 올바르게 모든 정보를 볼 수 나타나지만, 그것이 무엇을 기대하는지는 C가 무엇을 의미하는지 이해하지 못하는 것입니다.

신속하게 올바르게 호출하려면 어떻게해야합니까? 스위프트는

VDataStore.instance().askForMe() { 
    person, error in 
    // do things with myself that aren't strange 
} 

되는

답변

5

직접 문제가 askForMe 예를 방법입니다하지만 당신은 클래스 객체 VDataStore.askForMe에서 액세스하는 것을 당신의 ObjC의 호출 코드를 변환합니다. Swift는 인스턴스를 입력으로 가져 오는 함수 객체를 제공합니다.

+0

내가 그것을 놓쳤다 고 믿을 수 없다, 그것을 해결했다. 고맙습니다. – domitall