Untitled
var students: [[String: Any]] = [ ["firstName": "John", "lastName": "Wilson", "gpa": 2.4], ["firstName": "Nancy", "lastName": "Smith", "gpa": 3.5], ["firstName": "Michael", "lastName": "Liu", "gpa": 3.1] ] var highestGPAStudent: [String: Any]? for student in students { if let currentGPA = student["gpa"] as? Double { if let maxGPAStudent = highestGPAStudent, let maxGPA = maxGPAStudent["gpa"] as? Double { if currentGPA > maxGPA { highestGPAStudent = student } } else { highestGPAStudent = student } } } if let highestGPAStudent = highestGPAStudent, let firstName = highestGPAStudent["firstName"] as? String, let lastName = highestGPAStudent["lastName"] as? String { print("Student with the highest GPA: \(firstName) \(lastName)") } else { print("No student records found.") }
Leave a Comment