0

I am using xcode 9 beta 5 .I trying to parse the url .While maintaining session based on url the code is giving syntax error .

 static func fetchFeatureApp(){
        let urlString="http://ebmacs.net/ubereats/Api/all_product?id=1"
        URLSession.shared.dataTask(with: NSURLRequest(url: urlString)) { (data, responce, error) in
            if error!=nil
            {
                print(error)
                return

            }
        }.resume()
    }

image description here

URLSession.shared.dataTask(with: NSURLRequest(url: urlString)) { (data, responce, error) in

This line of code is giving error

Cannot convert value of type 'String' to expected argument type 'URL' How to correct this

How I can remove this error ? I have visited this link

2 Answers2

0

URLRequest expects an URL rather than a String, that's what the error message states.

let urlString = "http://ebmacs.net/ubereats/Api/all_product?id=1"
let url = URL(string: urlString)!
URLSession.shared.dataTask(with: URLRequest(url: url)) { (data, responce, error) in ...

You visited link clearly creates the URL from the string.

But actually you don't need an URLRequest for a simple GET request, just pass the URL

let url = URL(string: "http://ebmacs.net/ubereats/Api/all_product?id=1")!
URLSession.shared.dataTask(with: url) { (data, responce, error) in ...
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Hi here is my favourite code for urlsession download,

var request = URLRequest(url: URL(string: "Your URL String")!)

request.httpMethod = "POST"
let session = URLSession.shared
session.dataTask(with: request) {data, response, error in
     let parsedData = try JSONSerialization.jsonObject(with: data!) //if you want to convert to Json  
 }.resume()
arunjos007
  • 4,105
  • 1
  • 28
  • 43