gpt4 book ai didi

Objective-C:将选择器作为参数传递然后调用它

转载 作者:太空狗 更新时间:2023-10-30 03:36:30 25 4
gpt4 key购买 nike

我正在尝试将选择器作为参数传递并稍后执行。但是当我尝试使用控制台中的下一个错误调用选择器时出现 SIGABRT 错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HttpRequest OnFinishConn:]: unrecognized selector sent to instance 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]);
}

我是 objective c 的新手,所以请耐心等待。

最佳答案

那是因为您正在尝试使用 target-action模式但没有通过目标。我实际上会推荐委托(delegate)方法,但这里是您的界面应该是什么样子的示例。

@interface HttpRequest : NSObject
{
@private
id onEndTarget; //<- need to know who to send the message to (do not retain)
SEL onEndSel;
NSMutableData* receivedData;
}

//Accept the target here
-(void) StartRequest:(NSString *) url
parameters:(NSString*) params
onEndTarget:(id)target
onEndSelector:(SEL) selector;

//... implementation

-(void) connectionDidFinishLoading:(NSURLConnection*) connection
{
[onEndTarget performSelector:onEndSel withObject:[[NSMutableData alloc] initWithData:receivedData]];
}

//...
//Inside App Delegate
[req StartRequest:url parameters:@"a" onEndTarget:self onEndSelector:@selector(OnFinishConn:)];

确保您不保留 onEndTarget,因为这可能会造成保留循环。还可以考虑使目标成为一个属性,这样如果在连接完成之前对连接做了一些事情,它就可以不再是委托(delegate)。

关于Objective-C:将选择器作为参数传递然后调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7078572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com