在我的应用中,我使用AFNetwork来呼叫服务。这是我第一次使用AFNetwork。当我尝试通过查看一些教程来执行代码时,我遇到了一些错误:
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 :(得分:0)
我认为将错误消息的一部分发送到----的不兼容块指针类型。你有剩下的信息吗?
<强>建议强>
您可以查看Apple的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,其中包含使用块进行类型转换。您的错误似乎在抱怨块的类型转换问题。如果您无法解决,请发布更完整的错误消息,我将再看看。我已经把样本包括在内了,因为它迫使我思考这个问题。