I am using contains function in a function which array is a string array, like this:
func addArrayCheck(array: Array<String>, value: String) {
if (array.contains(value)) {
print("Element exists!")
}
else {
print("Element does not exists!")
}
}
Then I tried to make an extension, but Xcode complain about this:
Missing argument label 'where:' in call
extension Array {
func addArrayCheck(value: Self.Element) {
if self.contains(value) {
print("Element exists!")
}
else {
print("Element does not exists!")
}
}
}
Why I cannot use contains like I used in function in generic form? and how can I solve the issue?
addArrayCheck
to add elements, only if they're already not present in the array?contains(_:)
has linear (O(array.count)
) time complexity. If you're sure your array won't get long, it's fine, but I'd generally suggest just using aSet
, or anOrderedSet
if you need to preserve element order.