2014-07-05 1 views
1

블록이있는 하위 클래스 개체를 사용하는 방법을 이해하는 데 문제가 있습니다. 다음은 내가 시도하는 것의 예입니다. PFItem은 PFObject의 하위 클래스입니다.fetchIfNeededInBackgroundWithBlock과 같은 블록에서 PFObject의 하위 클래스를 사용하는 데 문제가 있습니다.

- (void) handleItem:(PFItem *)item{ 
    [item fetchIfNeededInBackgroundWithBlock:^(PFItem *item, NSError *error) { 
     if (!error) { 
      if ([item.name isEqualToString:@"Antidote"]) { 
       NSLog(@"Applied %@", item.name); 
       NSMutableArray *discardItems = [NSMutableArray array]; 
       for (PFItem *item in self.pfButtonCharacter.itemsApplied) { 
        if (item.malicious) { 
         [discardItems addObject:item]; 
         NSLog(@"Removing %@", item.name); 
        } 
       } 
       [PFObject deleteAll:discardItems]; 
      } 
     } 
    }]; 
} 

그러나, 의미 론적 오류로 엑스 코드 플래그이 :

내가 fetchIfNeededInBackgroundWithBlock에 PFObject에 PFItem에서 변경하는 경우

Incompatible block pointer types sending 'void (^)(PFItem *__strong, NSError *__strong)' to parameter of type 'PFObjectResultBlock' (aka 'void (^)(PFObject *__strong, NSError *__strong)')

, 그것은 작동하지 않습니다,하지만 나는 더 이상 항목의 속성에 액세스 할 수 있습니다. item.name 대신 item[@"name"]을 수행해야합니다. 당신이 수행하고있는 PFObjectPFItem A를하고

답변

2

PFItem 인수 대신 PFObject 인수를 사용하는 블록을 사용해야한다고 지정하는 경우 해당 방법과 일치하는 블록을 사용해야합니다. 이 답변은 광산과 같은 생각을 갖고

[item fetchIfNeededInBackgroundWithBlock:^(PFObject *obj, NSError *error) { 
    PFItem *item; 
    if ([obj isKindOfClass:[PFItem class]]) { 
     item = (PFItem *)obj; 
    } else { 
     return; 
    } 
    if (!error) { 
     if ([item.name isEqualToString:@"Antidote"]) { 
      NSLog(@"Applied %@", item.name); 
      NSMutableArray *discardItems = [NSMutableArray array]; 
      for (PFItem *item in self.pfButtonCharacter.itemsApplied) { 
       if (item.malicious) { 
        [discardItems addObject:item]; 
        NSLog(@"Removing %@", item.name); 
       } 
      } 
      [PFObject deleteAll:discardItems]; 
     } 
    } 
}]; 
+0

두 답변 모두 훌륭하지만, 캐스팅하기 전에 클래스를 확인하는 것이 안전하기 때문에 제공하고 있습니다. 둘 다 감사합니다! – user3741732

1
- (void) handleItem:(PFItem *)item{ 
[item fetchIfNeededInBackgroundWithBlock:^(PFObject *pfObj, NSError *error) { 
    PFItem *item = (PFItem *)pfObj; 
    if (!error) { 
     if ([item.name isEqualToString:@"Antidote"]) { 
      NSLog(@"Applied %@", item.name); 
      NSMutableArray *discardItems = [NSMutableArray array]; 
      for (PFItem *item in self.pfButtonCharacter.itemsApplied) { 
       if (item.malicious) { 
        [discardItems addObject:item]; 
        NSLog(@"Removing %@", item.name); 
       } 
      } 
      [PFObject deleteAll:discardItems]; 
     } 
    } 
}]; 
} 

캐스트. 이것은 PFObject이 실제로는 PFItem이라고 가정합니다.

+0

을하지만,'pfObj' 인수 ISN 경우 충돌합니다 :

당신이 전송되는 객체가 실제로 PFItem 알고 있다면, 당신은 항상 블록 내에서 전송할 수 있습니다 실제로는'PFItem'입니다. – nhgrif

+0

그 이유는 면책 조항입니다. – duci9y

+0

그래, 그냥'PFItem'이 아니라'pfObj'의 결과가 작동하지 않는 것 같아서 명확성을 추가하고있었습니다. 나는 객체가 확실히'PFItem '이 될지에 대해 어떤 보장도 할 수 있는지를 알기 위해 Parse에 대해 충분히 알지 못하지만, 그들의 메소드가'PFObject' 인수를 취하는 블록을 지정한 경우처럼 보인다. PFItem이라고 보장 할 수는 없습니다. – nhgrif