swift - 函数指针的应用 - 避免重复算法。
class DataSourceBase :EmployeeDataSource {
var employees = [Employee]();
func searchByName(name: String) -> [Employee] {
return search({e -> Bool in
return e.name.rangeOfString(name) != nil;
});
}
func searchByTitle(title: String) -> [Employee] {
return search({e -> Bool in
return e.title.rangeOfString(title) != nil;
})
}
private func search(selector:(Employee ->Bool)) -> [Employee] {
var results = [Employee]();
for e in employees {
if (selector(e)) {
results.append(e);
}
}
return results;
}
}