Swift: array of objects search

15,347

Solution 1

Finally after long search I did't ! , I was looking to find a way to do a dynamic search like if array of String contains

"hello","lo","yes"

and I want to get all the strings that contains for example "lo" I want to get "hello" and "lo"

so the best way I found is regular expression search

so I do a For Loop throw all options in Array and compare every single object variable to the pattern ,and save it in new array on objects

    for var i = 0; i < search_options_array.count; i++ {
        let myRegex = "searched_text"
        if let match = search_options_array[i].option.rangeOfString(myRegex, options: .RegularExpressionSearch){
            filtered_options_array.append(search_options(id:search_options_array[i].id,option:search_options_array[i].option) )
        }
    }

The best part here you can use all benefits of regular expression and have a copy of yours old array so if you need it.

Thanks every one for helping.

Solution 2

Find index of specific object:

if let index = find(myArray, objectIAmLookingFor) {
    // found! do something
}

Filter array:

let filteredArray = filter(myArray) { $0 == objectIAmLookingFor }

Solution 3

The correct answer is

func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
    let filteredArray = filter(search_options_array) { $0.option == "test" }
    println(searchBar.text)
}

or

func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
    let filteredArray = filter(search_options_array) { $0.id == "test" }
    println(searchBar.text)
}

You must retrieve property of searched object by which you perform searching

Solution 4

Because filter accepts as a predicate a function which maps each element of the given Array to a Bool value (to determine which value should be filtered out), in your case it may be this way;

let a = [
    search_options(id: "a", option: "X"),
    search_options(id: "b", option: "Y"),
    search_options(id: "c", option: "X")
]

let b = filter(a) { (e: search_options) in e.option == "X" }
// ==> [search_options(id: "a", option: "X"), search_options(id: "c", option: "X")]
Share:
15,347

Related videos on Youtube

Abdulaziz Noor
Author by

Abdulaziz Noor

mmmm I don't know what to say,,, Programmer maybe?

Updated on June 05, 2022

Comments

  • Abdulaziz Noor
    Abdulaziz Noor almost 2 years

    I want to search in array of objects in swift but I didn't know how :(

    I tried

    filteredArrayUsingPredicate
    

    but still don't work ,It's giving me an error msg

    -- Update --

    the error message is

    swift:42:9: 'Array<search_options>' does not have a member named 'filteredArrayUsingPredicate'
    

    -- Update --

    class search_options {
            let id:String
            let option:String
    
            init(){}
    
            init(id:String ,option:String){
                self.id = id
                self.option = option
            }
        }
    

    I only want to search in option variable

    And when I tried to used

    func searchBarSearchButtonClicked( searchBar: UISearchBar!)
    {
        let filteredArray = filter(search_options_array) { $0 == "test" }
        println(searchBar.text)
    }
    

    I got this message

    swift:40:58: 'search_options' is not a subtype of 'String'
    
    • Isuru
      Isuru
      I assume you're getting this error because filteredArrayUsingPredicate is an NSArray member. In Swift, you must be using the type Array which doesn't have that. Check out @fluidsonic's answer below to find out how to filter an Array using the new filter method.
  • Satyam
    Satyam almost 8 years
    How to check for bool?
  • Nikolai Nagornyi
    Nikolai Nagornyi over 7 years
    @Satyam what you mean?