Last active
April 27, 2022 10:22
-
-
Save Farhandroid/90db7abeff2a17b8ca75c724bef9e9a0 to your computer and use it in GitHub Desktop.
SearchUtil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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