gpt4 book ai didi

ios - 调用中的额外参数 'error'

转载 作者:行者123 更新时间:2023-11-28 06:37:22 24 4
gpt4 key购买 nike

我收到此错误Extra argument 'error' in call

上下文中的代码

   var post:NSString = "name=\(Username)&email=\(Email)&phone=\(phonenumb)&password=\(Password)&address=\(address)"

NSLog("PostData: %@",post);

var url:NSURL = NSURL(string: "http://userregistration.php")!

var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

var postLength:NSString = String( postData.length )

var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")


var reponseError: NSError?
var response: NSURLResponse?

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)


if ( urlData != nil ) {
let res = response as! NSHTTPURLResponse!;

NSLog("Response code: %ld", res.statusCode);

if (res.statusCode >= 200 && res.statusCode < 300)
{
var responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

NSLog("Response ==> %@", responseData);

var error: NSError?

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary

let success:NSInteger = jsonData.valueForKey("success") as! NSInteger

//[jsonData[@"success"] integerValue];

NSLog("Success: %ld", success);

if(success == 1)
{
NSLog("Sign Up SUCCESS");
self.dismissViewControllerAnimated(true, completion: nil)
} else {
var error_msg:NSString

if jsonData["error_message"] as? NSString != nil {
error_msg = jsonData["error_message"] as! NSString
} else {
error_msg = "Unknown Error"
}
var alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = error_msg as String
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()

}

} else {
var alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = "Connection Failed"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
} else {
var alertView:UIAlertView = UIAlertView()
alertView.title = "Sign in Failed!"
alertView.message = "Connection Failure"
if let error = reponseError {
alertView.message = (error.localizedDescription)
}
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}

我的错误出现在两个地方。

The first one

  var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)

The second one

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary

我试过以下方法

do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}

但是我导致错误如下

used unresolved jsonData

现在任何人都可以帮助我如何将这个 do catch 添加到我上面的原始代码中以纠正错误。

最佳答案

改变

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)

let urlData = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)

并改变

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary

let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options: []) as! NSDictionary

你的完整代码将是:

let url:NSURL = NSURL(string: "http://userregistration.php")!

let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

let postLength:NSString = String( postData.length )

let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")


let reponseError: NSError?
var response: NSURLResponse?

do {
let urlData = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)

if ( urlData != nil ) {
let res = response as! NSHTTPURLResponse!;

NSLog("Response code: %ld", res.statusCode);

if (res.statusCode >= 200 && res.statusCode < 300)
{
let responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

NSLog("Response ==> %@", responseData);


do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options: []) as! NSDictionary

let success:NSInteger = jsonData.valueForKey("success") as! NSInteger

//[jsonData[@"success"] integerValue];

NSLog("Success: %ld", success);

if(success == 1)
{
NSLog("Sign Up SUCCESS");
self.dismissViewControllerAnimated(true, completion: nil)
} else {
var error_msg:NSString

if jsonData["error_message"] as? NSString != nil {
error_msg = jsonData["error_message"] as! NSString
} else {
error_msg = "Unknown Error"
}
let alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = error_msg as String
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()

}

} catch let error as NSError {
print("json error: \(error.localizedDescription)")
}



} else {
let alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = "Connection Failed"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
}
}

关于ios - 调用中的额外参数 'error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38843431/

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