简体   繁体   English

提交表格时未使用表达结果

[英]Expression result unused when submitting form

I know this has been asked before, but none of the answers have solved my problem. 我知道以前已经有人问过这个问题,但是没有一个答案能解决我的问题。 Here's the code I'm using to send a post request 这是我用来发送帖子请求的代码

NSString *nameField = _name.text;
NSString *emailField = _email.text;
NSString *usernameField = _username.text;
NSString *passwordField = _password.text;

NSURL *url = [NSURL URLWithString:@"http://localhost/api/register"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSData *requestData = [@"name=%@&email=%@&username=%@&password=%@",nameField, emailField, usernameField, passwordField dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod:@"POST"];
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

This line specifically gives me problems 这条线专门给我带来了问题

NSData *requestData = [@"name=%@&email=%@&username=%@&password=%@",nameField, emailField, usernameField, passwordField dataUsingEncoding:NSUTF8StringEncoding];

The warning I get with it is 我得到的警告是

Expression result unused 表达结果未使用

Also I'd expect to get a result with my JSON so when there would be an error it would show it (backend part is done, it's just a matter of displaying it on iOS). 我也希望能用JSON获得结果,所以当出现错误时,它将显示出来(后端部分已经完成,只需要在iOS上显示即可)。 I'm not even sure if it's submitting 我什至不确定它是否正在提交

You forgot to use +stringWithFormat: when defining requestData . 您在定义requestData时忘记使用+stringWithFormat: requestData Change that line to this: 将该行更改为此:

NSData *requestData = [[NSString stringWithFormat:@"name=%@&email=%@&username=%@&password=%@", nameField, emailField, usernameField, passwordField] dataUsingEncoding:NSUTF8StringEncoding];

Also, you'll probably get the same warning on your last line. 另外,您可能会在最后一行收到相同的警告。 You can get rid of it by assigning it to something: 您可以通过将其分配给某些对象来摆脱它:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

You may also want to implement some of the NSURLConnection delegate methods. 您可能还想实现一些NSURLConnection委托方法。 See this tutorial for some helpful info. 有关一些有用的信息,请参见本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM