2014-10-28 1 views
1

사용자가 이미 앱에 등록 된 친구가 있는지 확인하는 방법을 만들고 있습니다.pfcloud 함수 내 부울 반환

-(BOOL)hasFriend:(NSArray*)phoneNums{ 
    [PFCloud callFunctionInBackground:@"checkUsers" 
            withParameters:@{@"array": phoneNums} 
              block:^(id success, NSError *error) { 
               if(success){ 
               return YES; 
               } 
               else{ 
               return NO; 
                }]; 
    } 

나는 또한이 시도했습니다 : 나는 아래의 코드를 실행할 때 Incompatible block pointer types sending 'BOOL (^)(__strong id, NSError *__strong)' to parameter of type 'PFIdResultBlock' (aka 'void (^)(__strong id, NSError *__strong)')

이것은 :이 블록 내에서 반환하기 위해 노력하고있어 그러나, 나는 말 컴파일러 오류가

-(BOOL)hasFriend:(NSArray*)phoneNums{ 
    __block bool hasFriend = nil; 

    [PFCloud callFunctionInBackground:@"checkUsers" 
        withParameters:@{@"array": phoneNums} 
          block:^(id success, NSError *error) { 
           if(success){ 
           hasFriend = YES; 
           NSLog(@"found a florin user!"); 
           } 
           else{ 
           hasFriend = NO; 
           } 
           } 
          }]; 

    NSParameterAssert(hasFriend); 
    return hasFriend; 
} 

그러나 이것은 절대로 NSParameterAssert을 전달하지 않습니다.

블록은 함수가 그래서 당신은 아마도 함수에서 값을 반환 할 수 반환 할 때까지 실행되지 않습니다
+0

첫 번째 방법은 반환 유형이 void 인 블록을 생성하지만 BOOL을 반환하려고하기 때문에 충돌이 발생합니다. 나는 NSParameterAssert 함수에 익숙하지 않지만'Assertions는 조건을 평가하고 조건이 false로 평가되면 현재 스레드에 대한 어설 션 처리기를 호출합니다. '그래서'hasFriend'는'NO'이고 NSParameterAssert 함수는이 값을 false로 처리하고 어설 션 처리기를 호출하려고합니다. 이 기능을 사용하는 특별한 이유가 있습니까? – AMI289

답변

2

... 당신은 함수에서 콜백 블록 (다른)를 사용해야합니다

-(void) hasFriend:(NSArray*)phoneNums withCallback:(void(^)(BOOL hasFriend))callback { 

    [PFCloud callFunctionInBackground:@"checkUsers" 
         withParameters:@{@"array": phoneNums} 
           block:^(id success, NSError *error) { 

            if (success) 
             callback(YES); 
            else 
             callback(NO); 

           }]; 

} 

그리고 그것을 사용하는,이 같은 콜백을 제공해야합니다 :

[myObj hasFriend:nums withCallback:^(BOOL hasFriend) { 

    if (hasFriend) 
     NSLog(@"Found friend!"); 
    else 
     NSLog(@"Friend not found..."); 

}]; 
1

PFIdResultBlock에는 반환 형식 (**void** (^)(__strong id, NSError *__strong)가 없습니다. "callFunctionInBackground"는 비동기 작업입니다. return hasFriend 줄에는 실제로 필요한 결과를 얻을 수 없다는 의미입니다. 대신 사용 방법 - 당신이 블록을 좋아하지 않는 경우에

그냥이

- (void)checkHasFriendWithNums:(NSArray *)phoneNums 
{ 
    [PFCloud callFunctionInBackground:@"checkUsers" 
        withParameters:@{@"array": phoneNums} 
          block:^(id success, NSError *error) { 
           if(success){ 
           hasFriend = YES; 
           NSLog(@"found a florin user!"); 
           } 
           else{ 
           hasFriend = NO; 
           } 
           if(completion) // perform the block 
           completion(hasFriend); 
           } 
          }]; 
} 

- (void)someMethodWithPhoneNums:(NSArray *)phoneNums 
{ 
    [self checkHasFriendWithPhoneNums:phoneNums completionBlock:^(BOOL hasFriend){ 
     if (hasFriend) 
     { 
      // do something 
     } 
    }]; 
} 

같은 방법 매개 변수로 완료 블록을 밀어 넣습니다.

- (void)checkHasFriendWithNums:(NSArray *)phoneNums completionBlock:(void(^)(BOOL hasFriends))completion 
{ 
    [PFCloud callFunctionInBackground:@"checkUsers" 
        withParameters:@{@"array": phoneNums} 
          block:^(id success, NSError *error) { 
           if(success) 
           [self hasFriends]; 
           else 
           [self hasNoFriends]; 
           } 
          }]; 
} 

- (void)hasFriends 
{ 
    // do something 
} 

- (void)hasNoFriends 
{ 
    // do something 
} 
+0

주의 사항 : 여기에 완성 블록을 실제로 전달하지 않고 대신 캡처 할 수 있습니다. – ravron

+0

도 완료 블록없이 처리하는 방법이 있습니까? 그래서 그것을'if ([self hasFriends : numbers]) {}'라고 부를 수 있을까요? – bdv

+0

@bdv 완료 블록 대신 클래스의 일부 메서드를 호출 할 수 있습니다. – Astoria