'How do you store your swift API data?
I am trying to store the token into a data model that I have set up.
I am very new to this, and so far if I print() I can see that I have the values, however they are not being stored in my Data().token.
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
class MyRequestController {
func getToken() {
let semaphore = DispatchSemaphore (value: 0)
var request = URLRequest(url: URL(string: "URL")!,timeoutInterval: Double.infinity)
request.addValue("Basic AUTH", forHTTPHeaderField: "Authorization")
request.addValue("AWSALB=efzS5NSzJXZ4OpGhrFJLjdemrPBlAW5H36T6T5AVneKb+5F9PGVi1VMmBpGe8SKom145AhE2MM6vZAvLGFVQezt2qG6hC8a12mshJewTwJYJFq9YCDfkK49kRhdE; AWSALBCORS=efzS5NSzJXZ4OpGhrFJLjdemrPBlAW5H36T6T5AVneKb+5F9PGVi1VMmBpGe8SKom145AhE2MM6vZAvLGFVQezt2qG6hC8a12mshJewTwJYJFq9YCDfkK49kRhdE", forHTTPHeaderField: "Cookie")
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request) { [self] data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
semaphore.signal()
do {
let jsonData = try JSONDecoder().decode(TokenInfo.self, from: data)
Data().token = jsonData.token
}catch{
print(error)
}
}
task.resume()
semaphore.wait()
}
struct TokenInfo: Decodable {
let token: String
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
