Last active
March 11, 2021 13:29
-
-
Save gozzoo/2f46624c4f7130350c372ff0b98d7201 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let api = "https://jsonplaceholder.typicode.com/posts/1" | |
let url = URL(string: api)! | |
let task = URLSession.shared.dataTask(with: url) { data, urlResponse, error in | |
if let data = data, let json = String(data: data, encoding: .utf8) { | |
print(json) | |
} | |
} | |
task.resume() | |
RunLoop.main.run(until: Date(timeIntervalSinceNow: 2)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let api = "https://jsonplaceholder.typicode.com/posts/1" | |
if let url = URL(string: api) { | |
let list = try? String(contentsOf: url) | |
if let data = list { | |
print(type(of: data), data) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let api = "https://jsonplaceholder.typicode.com/posts/1" | |
let url = URL(string: api)! | |
let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in | |
if let localURL = localURL { | |
if let string = try? String(contentsOf: localURL) { | |
print(string) | |
} | |
} | |
} | |
task.resume() | |
RunLoop.main.run(until: Date(timeIntervalSinceNow: 2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let params = ["username":"john", "password":"123456"] as Dictionary<String, String> | |
let url = "https://jsonplaceholder.typicode.com/posts/1" | |
var request = URLRequest(url: URL(string: url)!) | |
request.httpMethod = "POST" | |
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
let session = URLSession.shared | |
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in | |
print(response) | |
do { | |
let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary<String, AnyObject> | |
print(json) | |
} catch { | |
print("error") | |
} | |
}) | |
task.resume() | |
RunLoop.main.run(until: Date(timeIntervalSinceNow: 2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let dic = ["2": "B", "1": "A", "3": "C"] | |
let encoder = JSONEncoder() | |
if let jsonData = try? encoder.encode(dic) { | |
if let jsonString = String(data: jsonData, encoding: .utf8) { | |
print(jsonString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment