-1

I am fairly new to Swift but I have a function that returns a key value pair result of numbers

func dataPostRequest(_ url:String,_ paramString:String)
{
    let url:NSURL = NSURL(string: url)!
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url as URL)
    request.httpMethod = "POST"

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

    let task = session.dataTask(with: request as URLRequest) {
        (
        data, response, error) in

        guard let _:NSData = data as NSData?, let _:URLResponse = response, error == nil else {
            print("error")
            return
        }

        if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        {
            print(dataString)


        }
    }

    task.resume()

}

I am able to call the function like this:

dataPostRequest("https://myserver.com/","user_email=emailtest.com")

This works correctly but I want to now use the data that I've pulled and display it in a Table View. I have my tableView set up correctly but I am wondering how I can take my function and turn it into a key value pair array or a dictionary that I can use. I have tried creating an empty dictionary and set my function call to it but I get an error:

var emptyDictionary = [Int: Int]()

emptyDictionary = dataPostRequest("https://myserver.com/","user_email=emailtest.com")

And no matter what variation I try I keep getting:

Cannot assign value of type '()' to type '[Int : Int]' 

Any guidance would be greatly appreciated.

Nick
  • 1,036
  • 2
  • 14
  • 27
  • `URLSession. dataTask` is asynchronous. Use completion handlers – mag_zbc Feb 26 '20 at 16:14
  • 1
    Does this answer your question? [function with dataTask returning a value](https://stackoverflow.com/questions/40014830/function-with-datatask-returning-a-value) – mag_zbc Feb 26 '20 at 16:17

1 Answers1

1

dataPostRequest has no return value (aka ()). You can decode the received data in the completion handler and assign it to the dictionary. See the comment line below.

If you need to proceed in another function you have to add a completion handler described here.

Basically don't use NS... classes in Swift at all if there are native counterparts. And don't annotate types the compiler can infer.

The native Swift version is

func dataPostRequest(with url:String, paramString : String)
{
    let url = URL(string: url)!
    let session = URLSession.shared

    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    request.httpBody = paramString.data(using: .utf8)

    let task = session.dataTask(with: request) { data, response, error in

        if let error = error {
            print(error)
            return
        }

        let dataString = String(data: data!, encoding: .utf8)!
        print(dataString)

        // here decode the data to the desired type and assign it to emptyDictionary
    }

    task.resume()
}
vadian
  • 274,689
  • 30
  • 353
  • 361