gpt4 book ai didi

ios - 提交表单时未使用表达式结果

转载 作者:行者123 更新时间:2023-11-29 02:36:27 25 4
gpt4 key购买 nike

我知道以前有人问过这个问题,但没有一个答案能解决我的问题。这是我用来发送帖子请求的代码

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];

这一行特别给我带来了问题

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

我得到的警告是

Expression result unused

此外,我希望通过我的 JSON 获得结果,因此当出现错误时它会显示它(后端部分已完成,只是在 iOS 上显示它的问题)。我什至不确定它是否正在提交

最佳答案

您在定义requestData时忘记使用+stringWithFormat:。将该行更改为:

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

此外,您可能会在最后一行收到相同的警告。您可以通过将其分配给某些东西来摆脱它:

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

您可能还想实现一些 NSURLConnection 委托(delegate)方法。参见 this tutorial获取一些有用的信息。

关于ios - 提交表单时未使用表达式结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26310520/

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