2013-10-24 58 views
0

在我的應用程序中,我使用AFNetwork來調用服務。這是我第一次使用AFNetwork。當我試圖通過看一些教程,我得到了一些錯誤做代碼:不兼容的塊指針類型sendeing'void(^)(nsurlrequest)?

Incompatible block Types sending `void(^)(NSUrlRequest* _strong)…` 

我的代碼是

NSString *weatherUrl = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString]; 
    NSURL *url = [NSURL URLWithString:weatherUrl]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    // 2 
    AFJSONRequestOperation *operation = 
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    // 3 
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
      self.weather = (NSDictionary *)JSON; 
      self.title = @"JSON Retrieved"; 
      [self.tableView reloadData]; 
     } 
    // 4 
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
      UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" 
                 message:[NSString stringWithFormat:@"%@",error] 
                 delegate:nil 
              cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [av show]; 
     }]; 

    // 5 
    [operation start]; 

回答

0

我認爲會有要...錯誤的部分消息如不兼容的塊指針類型發送---到----。你有剩下的消息嗎?

建議

你可以看看蘋果的short practical guide on blocks。通常,不兼容的...類型錯誤意味着變量(在這種情況下的塊)形成不正確。解決此問題的最佳方法是在不同情況下使用塊,並提高您準確查看問題產生的位置的能力。您提供的代碼片段並不能顯示足夠的信息來直接回答問題。因此,這裏是做一個回調時,我會如何使用塊一個簡單的例子:

Class A.h 

typedef void (^CallbackBlock)(); 

@interface Class A : <NSObject> 
@property (strong, nonatomic) CallbackBlock onSomethingHappened; 
... 
@end 


ClassA.m 

@implementation ClassA 

-(void) someMethod 
{ 
    ... (after some work done) 
    self.onSomethingHappened(); //this will notify any observers 
} 
... 
@end 

ClassB.h 

#import "ClassA.h" 

@interface ClassB:UIViewController 
@property (strong, nonatomic)ClassA * referenceToClassA; 
@end 

ClassB.m 

@implementation ClassB 

//! Can also be some other method in the lifecycle 
- (void)viewDidLoad 
{ 
    __weak ClassB *weakSelf = self; 
    self.referenceToClassA.onSomethingHappened = ^(){ [weakSelf SomeMethodWithWorkTodoAfterSomethingHappened]; }; 

} 
... 
- (void)SomeMethodWithWorkTodoAfterSomethingHappened 
{ 
    ...do some work after receiving callback from ClassA 
} 

@end 

樣品

您可以查看以下blog post覆蓋類型轉換與塊。你的錯誤似乎是抱怨該塊的類型轉換問題。如果您無法解決問題,請發佈更完整的錯誤消息,我會再看一次。我已經包括了這個樣本,因爲它迫使我去思考這個問題。