我正在尝试将用户输入发送到php脚本,以便最终将其传递到sql服务器,大多数代码都在运行,但提交数据时遇到问题。

@IBAction func submit(_ sender: AnyObject) {

    let requestURL = URL(string: "*****")

    let request = NSMutableURLRequest(url:requestURL!)

    request.httpMethod = "POST"

    let song=txt1.text!
    let artist=txt2.text!
    let album=txt3.text!
    let year=txt4.text!
    let genre=txt5.text!


    let songPost = "song=" + (song as String)
    let artistPost = "&artist=" + (artist as String)
    let albumPost = "&album=" + (album as String)
    let yearPost = "&year=" + (year as String)
    let genrePost = "&genre=" +  (genre as String)

    request.httpBody = songPost.data(using: String.Encoding.utf8);
    request.httpBody = artistPost.data(using: String.Encoding.utf8);
    request.httpBody = albumPost.data(using: String.Encoding.utf8);
    request.httpBody = yearPost.data(using: String.Encoding.utf8);
    request.httpBody = genrePost.data(using: String.Encoding.utf8);
 --->>let task =  URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {                                                 // check for fundamental networking error
            print("error=\(error)")
            print(response)

            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = String(data: data, encoding: .utf8)
        print("responseString = \(responseString)")
    }
    task.resume()

代码的urlsession.shared.datatask行有问题。编译器错误显示“对成员的datatask的模糊引用(with:completionhandler:)”
什么可以使此代码正常工作?我如何验证此信息是否已在应用程序上传递?

最佳答案

您收到该错误消息的原因是,您正在需要NSMutableURLRequest的地方传递URLRequest
更改此行:

    let request = NSMutableURLRequest(url:requestURL!)

对此:
    var request = URLRequest(url:requestURL!)

应该会修好的。
但我建议您再做一些修复,以便将您的请求成功发送到服务器:
    let requestURL = URL(string: "*****")

    //You should use `URLRequest` in Swift 3, mutability is represented by `var`
    var request = URLRequest(url:requestURL!)

    request.httpMethod = "POST"

    //UITextField.text can be nil, you should treat nil cases
    //(Generally avoid using forced unwrapping `!` as far as you can.)
    let song = txt1.text ?? ""
    let artist = txt2.text ?? ""
    let album = txt3.text ?? ""
    let year = txt4.text ?? ""
    let genre = txt5.text ?? ""

    //`song`,... are all Strings, you have no need to add `as String`
    let songPost = "song=" + song
    let artistPost = "&artist=" + artist
    let albumPost = "&album=" + album
    let yearPost = "&year=" + year
    let genrePost = "&genre=" + genre

    //You need to make a single data containing all params
    //(Creating a concatenated String and getting `data` later would be another way.)
    var data = Data()
    data.append(songPost.data(using: String.Encoding.utf8)!)
    data.append(artistPost.data(using: String.Encoding.utf8)!)
    data.append(albumPost.data(using: String.Encoding.utf8)!)
    data.append(yearPost.data(using: String.Encoding.utf8)!)
    data.append(genrePost.data(using: String.Encoding.utf8)!)
    request.httpBody = data
    let task =  URLSession.shared.dataTask(with: request) { data, response, error in
       ...

07-26 09:37