i have to filter dictionary containing my text inside of textfield
//my code
var arrayData = [["Name":"Sahil"],["Name":"Raman"],["Name":"ashish"],["Name":"Vishnu"],["Name":"Deep"],["Name":"sahil"],["Name":"Swift"]]
//my result should be like this
[["Name":"Sahil"],["Name":"ashish"],["Name":"Vishnu"],["Name":"Swift"]]
2
Answers
This should work:
Here is the documentation for filtering:
https://developer.apple.com/documentation/swift/sequence/3018365-filter
It was unclear, you explained it later in comments, but you want to keep the values for key
"Name"
which contains an "s" case insensitive.Let’s add a couple of additional values to show why your previous attempts failed:
And simplify:
You attempted:
Output:
So, you get only when value is exactly equals to
"s"
, that’s normal, you are using==
You then attempted:
Output:
So, you are getting anyvalues when it has a
"s"
inside it. It’s almost that, but you aren’t getting when it has an uppercase"S"
.To do that, you can use
range(of:options:searchRange:local:)
with.caseInsensitive
optiono. If the result isn’tnil
, it means it has been found.So you can use:
Output: