Estoy tratando de pasar un selector como parámetro y ejecutarlo más tarde. Pero me sale un error de SIGABRT cuando intento llamar el selector con el siguiente error en la consola:Objetivo-C: Pase el selector como parámetro y luego llámelo
Terminación de aplicación debido a excepción no detectada 'NSInvalidArgumentException', razón: '- [HttpRequest OnFinishConn:]: selector no reconocido enviado a la instancia 0x7834c80'
HttpRequest.h
#import <Foundation/Foundation.h>
@interface HttpRequest : NSObject
{
@private SEL onEndSel;
@private NSMutableData* receivedData;
}
-(void) StartRequest:(NSString *) url
parameters:(NSString*) params
onEndSelector:(SEL) selector;
@end
HttpRequest.m
#import "HttpRequest.h"
@implementation HttpRequest
-(void) StartRequest:(NSString *)url
parameters:(NSString*)params
onEndSelector:(SEL)selector
{
receivedData = [[NSMutableData alloc] init];
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
onEndSel = selector;
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void) connectionDidFinishLoading:(NSURLConnection*) connection
{
//NSLog([[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]);
[self performSelector:onEndSel withObject:[[NSMutableData alloc] initWithData:receivedData]];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[receivedData appendData:data];
}
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
HttpRequest* req = [[HttpRequest alloc] init];
SEL mysel = @selector(OnFinishConn:);
NSString * url = [[NSString alloc] initWithString:@"http://www.google.es"];
[req StartRequest:url parameters:@"a" onEndSelector:@selector(OnFinishConn:)];
[self.window makeKeyAndVisible];
return YES;
}
-(void)OnFinishConn:(NSMutableData *)data
{
NSLog([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
Soy nuevo en Objective C, así que por favor sea paciente.
Si bien no es la respuesta a su pregunta, el hecho de que esta pregunta, me lleva a recomendar que pruebe ASIHttpRequest, en lugar de reinventar la rueda – Jonathan