If my main array is ["Hello","Bye","Halo"]
, and I'm searching for "lo"
, it will filter the array only to ["Hello", "Halo"]
.
This is what I've tried:
let matchingTerms = filter(catalogNames) { $0.rangeOfString(self.txtField.text!, options: .CaseInsensitiveSearch) != nil }
It throws
Type of expression is ambiguous without more context
Any suggestions?
To filter strings of an Array based on length in JavaScript, call Array. filter() method on this String Array, and pass a function as argument that returns true for the specific condition on string length or false otherwise.
If you want to put multiple conditions in filter , you can use && and || operator.
Use contains
instead:
let arr = ["Hello","Bye","Halo"] let filtered = arr.filter { $0.contains("lo") } print(filtered)
Output
["Hello", "Halo"]
Thanks to @user3441734 for pointing out that functionality is of course only available when you import Foundation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With