XCode 无法识别自定义方法

发布于 2024-11-09 21:58:40 字数 2020 浏览 0 评论 0原文

我有一个名为“GREST”的自定义 NSObject 类,除了这个方法之外,一切都有效。自动完成甚至无法识别它,老实说我不明白为什么。我尝试过更改方法的名称和所有内容。当我调用该方法时,它起作用了,我只是收到警告,该类可能不会响应该方法。

我在头文件中声明了这个方法

- (void)connect:(NSString *)connection_url parameters:(NSString *)parameter_string header:(NSString *)header_type

,这里是实现

- (void)connect:(NSString *)connection_url parameters:(NSString *)parameter_string header:(NSString *)header_type {

    NSData *request_data = [parameter_string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *request_length = [NSString stringWithFormat:@"%d", [request_data length]];

    response_data = [[NSMutableData alloc] init];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:connection_url]];
    [request setHTTPMethod:@"POST"];
    [request setValue:request_length forHTTPHeaderField:@"Content-Length"];
    if([header_type isEqualToString:@""] || header_type == nil || header_type == NULL) {
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    } else {
        [request setValue:header_type forHTTPHeaderField:@"Content-Type"];
    }
    [request setHTTPBody:request_data];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:10];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    if(!connection) {
        [self apicall_failed];
    }
    [connection release];

}

当我尝试调用这个方法时,XCode 给我以下警告

'GREST' may not respond to '-connect:parameter_string:header_type:'

编辑,这里是代码的调用方式:

GREST *api = [[GREST alloc] init];
[api setDelegate:self];
[api connect:@"http://domain.com" parameter_string:@"{'username':'hi'}" header_type:@"application/json"];
[api release];

一切正常,但是我现在只是想摆脱这个警告。关于我如何做到这一点有什么想法吗?提前致谢!

I have a custom NSObject class called "GREST", everything works aside from this method. Auto completion won't even recognize it, and I honestly can't figure out why. I've tried changing the method's name and everything. When I call the method, it works, I just get the warning that the class may not respond to this method.

I have this method declared in my header file

- (void)connect:(NSString *)connection_url parameters:(NSString *)parameter_string header:(NSString *)header_type

And here is the implementation

- (void)connect:(NSString *)connection_url parameters:(NSString *)parameter_string header:(NSString *)header_type {

    NSData *request_data = [parameter_string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *request_length = [NSString stringWithFormat:@"%d", [request_data length]];

    response_data = [[NSMutableData alloc] init];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:connection_url]];
    [request setHTTPMethod:@"POST"];
    [request setValue:request_length forHTTPHeaderField:@"Content-Length"];
    if([header_type isEqualToString:@""] || header_type == nil || header_type == NULL) {
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    } else {
        [request setValue:header_type forHTTPHeaderField:@"Content-Type"];
    }
    [request setHTTPBody:request_data];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:10];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    if(!connection) {
        [self apicall_failed];
    }
    [connection release];

}

When I try to call this method, XCode gives me the following warning

'GREST' may not respond to '-connect:parameter_string:header_type:'

Edit, here's how the code is being called:

GREST *api = [[GREST alloc] init];
[api setDelegate:self];
[api connect:@"http://domain.com" parameter_string:@"{'username':'hi'}" header_type:@"application/json"];
[api release];

Everything works fine, but I'm just trying to get rid of this warning now. Any ideas on how I can do this? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-11-16 21:58:40

比较方法名称:

'-connect:parameter_string:header_type:'

不同之处

'-connect:parameters:header:'

在于您使用的是方法参数的名称,而不是实际的方法/选择器。这将导致调用时出现运行时异常。

编译器会告诉你尝试使用前者的行,并且 xcode 可能会突出显示该行。

compare the method names:

'-connect:parameter_string:header_type:'

to

'-connect:parameters:header:'

the difference is that you are using the names of the method's arguments, not the actual method/selector. this will result in a runtime exception when called.

the compiler will tell you the line where you try to use the former, and xcode will probably highlight the line.

若相惜即相离 2024-11-16 21:58:40

[api connect:@"http://domain.com"parameter_string:@"{'username':'hi'}" header_type:@"application/json"];

更改为

[api 连接:@"http://domain.com" 参数:@"{'用户名':'hi'}" 标头:@"application/json"];

Change

[api connect:@"http://domain.com" parameter_string:@"{'username':'hi'}" header_type:@"application/json"];

to

[api connect:@"http://domain.com" parameters:@"{'username':'hi'}" header:@"application/json"];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文