Junior — Middle
Could you give an example of using a class from the Swift standard library?
sobes.tech AI
Answer from AI
One of the commonly used classes in the Swift standard library is URLSession. It allows you to perform network requests.
Example of using URLSession to download data from a website:
import Foundation
let url = URL(string: "https://api.example.com/data")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print("Error: \(error)")
return
}
if let data = data, let string = String(data: data, encoding: .utf8) {
print("Received data: \(string)")
}
}
task.resume()
This example shows how to asynchronously fetch data from the internet using the standard class.