Skip to content

Instantly share code, notes, and snippets.

@Farhandroid
Last active April 27, 2022 10:22
Show Gist options
  • Save Farhandroid/90db7abeff2a17b8ca75c724bef9e9a0 to your computer and use it in GitHub Desktop.
Save Farhandroid/90db7abeff2a17b8ca75c724bef9e9a0 to your computer and use it in GitHub Desktop.
SearchUtil
class SearchUtil{
var list: [Int] = []
init(list: [Int]) {
self.list = list
}
func searchItem(element: Int, foundItem: (Int?)->()) {
let itemFoundList = self.list.filter { item in
item == element
}
if (itemFoundList.isEmpty){
foundItem(nil)
}
else{
foundItem(itemFoundList.first)
}
}
}
let searchUtil = SearchUtil(list: [Int](arrayLiteral: 24,25,26))
searchUtil.searchItem(element: 23) {(result) -> () in
print(result ?? "Not found")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment