I am fairly new to Swift, and am trying to make an HTTP request. I tried many of the ideas in this Stack Overflow question, but all caused errors when run in a playground; I believe this is because they are all in Swift 1.0-2.0.
How can I make an HTTP request in Swift 3?
Update I tried the first solution presented in this answer and, after completing Xcode's suggested "Fix-its" I encountered four errors:
In your error it seems url missing.Here bare simple api call to help you get started with.
There's typo errors (no url variable in the call) in your code.
Anyway, in Swift 3 it's better to use the new URL struct and the URLSession class.
Also, XCPlayground is now PlaygroundSupport.
And no need to use NSString when String is available.
Note: this is for Xcode 8 beta 2. On beta 1 you would have to do
shared()
instead ofshared
. If you're on iOS don't forget toimport UIKit
, but this code also works for OS X if youimport Cocoa
instead.There are a couple problems with your code:
Info.plist
file to connect to HTTP sites.dataTask(urlwith: ! as URL)
. What are you trying to unwrap with the exclamation mark (!
)? What's the variable name?A lot of class names have changed between Swift 2 and 3 so those answers you've found may not be applicable. Below is an example that connects to httpbin.org to get your IP address: