XCode 无法识别自定义方法
我有一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
比较方法名称:
不同之处
在于您使用的是方法参数的名称,而不是实际的方法/选择器。这将导致调用时出现运行时异常。
编译器会告诉你尝试使用前者的行,并且 xcode 可能会突出显示该行。
compare the method names:
to
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.
将
[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"];