gpt4 book ai didi

ios - 从抛出函数类型 (_,_,_) throws 到非抛出函数类型 (Data?, Response?, URLResponse?) 的无效转换 Void

转载 作者:行者123 更新时间:2023-11-28 20:59:15 27 4
gpt4 key购买 nike

我是 Swift 编程的新手,我在“let task = URLSession”上遇到错误。我研究并发现了其中一些错误,但没有一个答案对我有用。这是我的代码,第 44 行抛出这个错误

import UIKit

class loginViewController: UIViewController {

@IBOutlet weak var userLoginTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!


override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func loginButtonTapped(_ sender: Any) {

let user_login = userLoginTextField.text;
let user_passwd = userPasswordTextField.text;

if((user_passwd?.isEmpty)! || (user_passwd?.isEmpty)!) { return; }

//Send user data to server
let myUrl = NSURL(string: "https://www.pcpusa.net/legacy/iOS/userLogin.php");
var request = URLRequest(url: myUrl! as URL);
request.httpMethod = "POST";

let postString = "login=\(String(describing: user_login))&password=\(String(describing: user_passwd))";

request.httpBody = postString.data(using: String.Encoding.utf8);

**let task = URLSession.shared.dataTask(with: request){**
data,response,error in
if error != nil {
print("error=\(String(describing: error))")
return
}

var err: NSError?
var json = try JSONSerialization.JSONObjectWithData(data, options: .mutableContainers, error: &err) as? NSDictionary

if let parseJSON = json {
var resultValue:String = parseJSON["status"] as String!;
print("result: \(resultValue)")

if(resultValue=="Success")
{

//Login is successful
NSUserDefaults.standardUserDefaults().setBool(true, value(forKey: "isUserLoggedIn"));
NSUserDefaults.standardUserDefaults().synchronize();

self.dismissViewControllerAnimated(true, completion:nil);

}

}

}
task.resume()
}
}

最佳答案

URLSession.dataTask期望一个非抛出的完成处理程序,并且您正在向它传递一个抛出的闭包。因此,它无法推断方法(换句话说:它找不到具有该名称的方法接受抛出的闭包)。

要解决此问题,您应该捕获并处理解码 JSON 时可能发生的错误。

关于ios - 从抛出函数类型 (_,_,_) throws 到非抛出函数类型 (Data?, Response?, URLResponse?) 的无效转换 Void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50412794/

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