3

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'
4
  • What error message? How to Ask Commented Oct 1, 2014 at 6:12
  • sorry I'm new here and english is not my primary language so I didn't know and it's hard for me to give lots if details but I will try Commented Oct 1, 2014 at 6:21
  • 1
    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. Commented Oct 1, 2014 at 6:24
  • thanks , I tried and I got new error message Commented Oct 1, 2014 at 6:34

4 Answers 4

11

Find index of specific object:

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

Filter array:

let filteredArray = filter(myArray) { $0 == objectIAmLookingFor }
Sign up to request clarification or add additional context in comments.

3 Comments

I get this error swift:40:24: Type 'search_options' does not conform to protocol 'Equatable'
If you want two instances of search_options to be comparable using == then the class (or struct) has to implement Equatable protocol.
OK I will search to how implement Equatable class ,Thanks for helping
3

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.

Comments

2

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")]

Comments

2

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

2 Comments

How to check for bool?
@Satyam what you mean?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.