swift - 函数指针的应用 - 避免重复算法

前端之家收集整理的这篇文章主要介绍了swift - 函数指针的应用 - 避免重复算法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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;

}

}

猜你在找的Swift相关文章