请原谅我的英语,我是法国人。
所以,我是iOS开发的新手。我开发了一种方法,它必须在异步任务之后调用回调函数。我已经搜索了选择器,但我无法真正理解它是如何工作的......
这是我的代码:
NSString *const BaseURLString = @"http://mywebsite.fr/";
+ (void) getAdverts:(SEL *)myselector {
// 1
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:BaseURLString]];
// 2
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
[client getPath:@"jsoncall"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonDict = (NSDictionary *) responseObject;
NSArray *adverts = [jsonDict objectForKey:@"Advert"];
NSMutableArray *advertList = [[NSMutableArray alloc] init];
Advert *advert = [[Advert alloc] init];
[adverts enumerateObjectsUsingBlock:^(id obj ,NSUInteger idx, BOOL *stop){
advert.identifier = [(NSNumber *)[obj objectForKey:@"id"] integerValue];
advert.title = [obj objectForKey:@"title"];
advert.size = [obj objectForKey:@"size"];
NSLog(@"ID:%d Size:%@", advert.identifier, advert.size);
[advertList addObject:advert];
}];
// Use selector to callback a function
[self performSelector:myselector withObject:@"test"];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[NSString stringWithFormat:@"%@",error]
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
}
];
}
“调用者”代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[ServerHelper getAdverts:@selector(mySelector)];
}
- (void)mySelector
{
NSLog(@"selector ok");
}
这里的错误:无法识别的选择器发送到类
非常感谢