import Foundation import Glibc var people: [[String:Any]] = [ [ "firstName": "Calvin", "lastName": "Newton", "score": 13 ], [ "firstName": "Garry", "lastName": "Mckenzie", "score": 23 ], [ "firstName": "Leah", "lastName": "Rivera", "score": 10 ], [ "firstName": "Sonja", "lastName": "Moreno", "score": 3 ], [ "firstName": "Noel", "lastName": "Bowen", "score": 16 ] ] func bestScore(_ arrOfPeople: [[String:Any]]) -> String { var personName = "" var personScore = 0 for person in arrOfPeople { let score = person["score"] as! Int if score > personScore { personScore = score let firstName = person["firstName"] as! String let lastName = person["lastName"] as! String personName = "\(firstName) \(lastName)" } } return personName } print(bestScore(people)) /* var topPerson = people[0] var bestScore = topPerson["score"] as! Int for person in people { if let score = person["score"] as? Int { if bestScore < score { bestScore = score topPerson = person } } } if let first = topPerson["firstName"] as? String, let second = topPerson["lastName"] as? String { print("\(first) \(second)") } */ //Tuples var person = ("ahmed", "ali", "test") print(person.0) print(person.2) var person2 = (firstName: "ahmed", age: 20 ) print(person2.firstName) print(person2.age) var (a, b, c) = (1, 2, 3) print(a) func dividmod(_ a: Int, _ b: Int) -> (Int, Int) { return (a / b , a % b) } print(dividmod(7, 3))
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more