在if语句中解决问题......
func sortStudents(students: [Student], byNames: [String] ) -> [Student]{
var result = [Student]()
for name in byNames {
if let students = students.filter{ $0.sName == name}{
if students.count > 0{
result.append(students[0])
}
}
}
return result
}
答案 0 :(得分:1)
理论上,它将是
if let students = students.filter({ $0.sName == name }) { ... }
但是filter
无论如何都不会返回可选项,因此您根本不应该使用if let
语法。
所以你可以这样做:
func sortStudents(students: [Student], byNames: [String] ) -> [Student]{
var result = [Student]()
for name in byNames {
let students = students.filter { $0.sName == name }
if students.count > 0 {
result.append(students[0])
}
}
return result
}