Currently, I have a Struct called User, which defines the variables I want to decode from the HTTP response. Then, I have an array called users which will contain the response. As you can see, I populate users in the random8 function, but now in the random1 function I am having problems. In the random1 function, I only get 1 user instead of 8, and I am trying to take that data and replace one of the current users with that new data. I am able to do that successfully in my code (by extracting each value), but I am pretty sure I am not doing it the standard way. Please let me know if there is a better way to do this.
Keep in mind, random8 is called on viewDidLoad, and random1 occurs later on when they tap a specific button, so I am replacing the data of one specific index of users.
struct User: Decodable {
var id: Int
var first_name: String
var last_name: String
var picture_url: String
var points: Int
var school: String
var grade: Int
}
var users = [User]()
func getRandom8() {
let url = URL(string: "https://somewebsite.com/users/random/eight")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
let preferences = UserDefaults.standard
request.addValue("JWT \(preferences.object(forKey: "token") as! String)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "GET"
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
do {
self.users = try JSONDecoder().decode([User].self, from: data!)
DispatchQueue.main.async (
execute: self.loadRandom8
)
} catch {
print("error in getting data")
}
})
task.resume()
}
func getRandom1(index:Int) {
let url = URL(string: "https://somewebsite.com/users/random/one")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
let preferences = UserDefaults.standard
request.addValue("JWT \(preferences.object(forKey: "token") as! String)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "GET"
//write a statement that says if that user is already there, do the call again
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
do {
let responseObject = (try? JSONSerialization.jsonObject(with: data!)) as? [String: Any]
print(responseObject)
let indexFirstName = responseObject?.index(forKey: "first_name")
let indexId = responseObject?.index(forKey: "id")
let indexPictureURL = responseObject?.index(forKey: "picture_url")
let indexPoints = responseObject?.index(forKey: "points")
let indexSchool = responseObject?.index(forKey: "school")
let indexGrade = responseObject?.index(forKey: "grade")
let indexLastName = responseObject?.index(forKey: "last_name")
self.users[index].last_name = responseObject![indexLastName!].value as! String
self.users[index].first_name = responseObject![indexFirstName!].value as! String
self.users[index].id = responseObject![indexId!].value as! Int
self.users[index].picture_url = responseObject![indexPictureURL!].value as! String
self.users[index].points = responseObject![indexPoints!].value as! Int
self.users[index].school = responseObject![indexSchool!].value as! String
self.users[index].grade = responseObject![indexGrade!].value as! Int
DispatchQueue.main.async (
execute: self.loadRandom8
)
} catch {
print("error in getting data")
}
})
task.resume()
}
getRandom1not useJSONDecoder? Why do you create all of thoseindexFirstName,indexId, etc. variable and then ignore them?