1

trying to send variables entered in text fields to a php script to write into an sql database. attempting to use HTTP request

@IBAction func submit(_ sender: AnyObject) {

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

    let request = NSMutableURLRequest(url: requestURL! as URL)

    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);
    NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.mainQueue)

I get an error on the bottom line of code reading: Missing argument for parameter 'completion hander' in call.

not sure what this means.

Dima
  • 23,484
  • 6
  • 56
  • 83
xteetsx
  • 43
  • 8
  • Don't use `NSURLConnection`. It's deprecated in iOS 9. http://stackoverflow.com/questions/32441229/nsurlconnection-deprecated-in-ios9 – FelixSFD Oct 17 '16 at 18:57

2 Answers2

0

The error is actually extremely clear here. This method has a third parameter called completionHandler which you are missing. Documentation. Replace your last line with this:

NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.mainQueue)
{
  response, data, error in
  // handle response here
}
Dima
  • 23,484
  • 6
  • 56
  • 83
  • 1
    You changed the question completely. I undid your change. If you have a new question you should post it separately. – Dima Oct 17 '16 at 21:08
  • I did post a new question sorry I'm new to this site!! i figured since it was similar in nature i could just edit it but I went ahead and posted a new one, thanks for the help – xteetsx Oct 17 '16 at 22:44
0

As I mentioned in the comment, you should not use NSURLConnection anymore. But I answer your question, because it might help you, if you get a similar error in the future.


The message says, that you are calling a function with less parameters than required. In this case, the completionHandler is missing.

NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.mainQueue, completionHandler: {
     response , data, error in
})

In Swift 3, you can make a completion handler like this, too:

NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main) {
    response , data, error in
    // your code here
}

BTW: In Swift, you don't need to use NSMutableURLRequest. URLRequest is already mutable by using var.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • What is go in //your code here? I haven't used this method before I stumbled across it on the internet but Xcode kept throwing errors at me, I'm new to xcode – xteetsx Oct 17 '16 at 19:13
  • @xteetsx there you can put the code that you want to execute, after the request finished. The HTTP-Request runs asynchronously (in the background). That means, that the line after `sendAsynchronousRequest()` will immediately be executed, but the code within the `completionHandler()` waits for the request to finish – FelixSFD Oct 17 '16 at 19:16
  • NSURLConnection.sendAsynchronousRequest(request), queue:**This was corrected to ';' now it says 'expected expression'*** OperationQueue.main) { response , data, error in // your code here } – xteetsx Oct 17 '16 at 19:16
  • are you using Swift 3? Or still 2.x? – FelixSFD Oct 17 '16 at 19:16
  • 1
    I've been using swift 3 – xteetsx Oct 17 '16 at 19:21
  • oh. I just saw a typo in my code :-D Remove the ) after `request` – FelixSFD Oct 17 '16 at 19:22
  • It says its expecting an expression where i put the ***'s is there a way to do this in swift 3? – xteetsx Oct 17 '16 at 19:24
  • Assuming that `request` is of type `URLRequest`, the second codepiece compiles without errors using Swift 3 – FelixSFD Oct 17 '16 at 19:27