0

I currently have a UITextField where I can enter a search string and it will return any of the strings in an array that contain that text, in that specific order. However, I want to make it possible to search on every word and ignore the order of the strings in the array.

var results = [String]()
let filterArray = ["Big green bubble", "Red bubble", "A bubble in green", "Small green bubble", "This bubble is green"]     
let textString = "green bubble" 
for i in 0 ..< filterArray.count { 
    let checkString = filterArray[i] 
    let filterRange = (checkString as NSString).range(of: textString, options: .caseInsensitive) 
    if filterRange.location != NSNotFound { 
        results.append(checkString) 
    }
}  
print(results) // returns ["Big green bubble", "Small green bubble"]

I wish it would also include "A bubble in green" and "This bubble is green", because it contains every word I am searching for. Any ideas?

2 Answers 2

2

Just filter elements from filterArray which contains both of these words

let filterArray = ["Big green bubble", "Red bubble", "A bubble in green", "Small green bubble", "This bubble is green"]
let textString = "green bubble".lowercased()
let words = textString.components(separatedBy: " ")
let results = filterArray.map { $0.lowercased() }.filter { string in words.allSatisfy { string.components(separatedBy: " ").contains($0) } }

print(results) /* ["Big green bubble", "A bubble in green", "Small green bubble", "This bubble is green"] */
4
  • 1
    for case insensitive let results = filterArray.filter { string in words.allSatisfy { string.range(of: $0, options: .caseInsensitive) != nil } }
    – Leo Dabus
    Commented Jan 15, 2019 at 21:31
  • @LeoDabus thanks! I didn't notice that op'd mentioned it in his question ;) Commented Jan 15, 2019 at 21:37
  • Neat! Thank you so much, that's amazing, clean and compact!
    – PennyWise
    Commented Jan 15, 2019 at 22:47
  • 1
    IF I have struct and I apply this to my key title it return array of titles but I want whole model array then how can i do this sir? please help me on this. Commented May 15, 2020 at 15:55
2

You can use a set and check if the intersection count is equal to the number of words in the query:

let filterArray = ["Big green bubble", "Red bubble", "A bubble in green", "Small green bubble", "This bubble is green"]
let textString = "green bubble"
let wordsSet = Set(textString.components(separatedBy: " "))
let queryResult = filterArray.filter {
    Set($0.components(separatedBy: " ")).intersection(wordsSet).count == wordsSet.count
}
queryResult  // "Big green bubble", "A bubble in green", "Small green bubble", "This bubble is green"]

If you need case insensitive you can map all the words to lowercase:

let wordsSet = Set(textString.lowercased().components(separatedBy: " "))
let queryResult = filterArray
    .map { $0.lowercased() }
    .filter {
    Set($0.components(separatedBy: " "))
        .intersection(wordsSet).count == wordsSet.count
}
5
  • I have now implemented Robert's answer because it is smaller and more compact, but this answer works perfectly fine as well so thanks a bunch for your input!
    – PennyWise
    Commented Jan 15, 2019 at 22:49
  • IF I have struct and I apply this to my key title it return array of titles but I want whole model array then how can i do this sir? please help me on this. Commented May 15, 2020 at 15:53
  • @YogeshPatel Would you ming posting a question with your attempt and the issues you are facing? The comments are it is not intended for questions.
    – Leo Dabus
    Commented May 15, 2020 at 21:50
  • sure I will Post Question and share link here. Thanks Commented May 16, 2020 at 7:05
  • @LeoDabus Please check this I posted question here stackoverflow.com/questions/61932628/… Commented May 21, 2020 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.