1

I have an array of objects and I want to filter them by two conditions, either if object is in an certain group or if it has an certain name but I can not figure out why it is not working.

This is my code:

let outputfiler = array.filter({$0.group.contains (where: {$0 == "groupBig" }) } || $1.name.contains(where: {$1 == "Eis"})  )

This is the error I get:

Anonymous closure argument not contained in a closure

Edit: Group is an array of trings, name just a string.

I also tried this:

outputfiler = array.filter{$0.group.contains (where: {$0 == "groupBig" }) || $0.name(where: {$0 == "Eis"}) }

But then I get this error:

Extraneous argument label 'where:' in call
3
  • Filter takes a single closure, you can have multiple boolean expressions within the closure. So start by ensuring your array.filter is followed by { rather than (. Commented Jan 25, 2018 at 18:00
  • I did the changes you suggested but does not work Commented Jan 25, 2018 at 18:05
  • if name is a String property, $0.name(where:) doesn't really make sense. Are you trying to test if contains the substring "Eis" or is equal to "Eis"? In the latter case, use array.filter{ $0.group.contains("groupBig") || $0.name == "Eis" }. In the former, contains(where:) only deals with Character so you need a different API Commented Jan 25, 2018 at 22:12

3 Answers 3

5

You need your filter as follows (shown on multiple lines for clarity):

let outputfiler = array.filter({
    $0.group.contains(where: { $0 == "groupBig" }) || 
    $0.name.contains("Eis")
})

You had the filter's closing } before the ||. And assuming name is a String, contains just takes the string to search, not a closure.

Sign up to request clarification or add additional context in comments.

3 Comments

i tried this but xcode tells me "Extraneous argument label 'where:' in call" i should delete the secont "where".
Is name a String or an array of String? See the update that assumes it is a String and not an array.
yes thanks sorry my fault, yes group was an array of strings and name just an string. It works now, thanks again
1

This syntax should work.

let outputfiler = array.filter{$0.group.contains("groupBig") || $0.name.contains("Eis")}

Comments

0

This is my solution in Swift 5 (Example 👇🏽👇🏽👇🏽):

My struct (I use this struct for extract data from Firebase):

struct User { 

    let uid:String
    let phoneNumber:String
    let username:String
    let email:String

    init(uid:String, dictionary: [String: Any]) {
        self.uid = uid
        self.username = dictionary["username"] as? String ?? String()
        self.phoneNumber = dictionary["phoneNumber"] as? String ?? String()
        self.email = dictionary["email"] as? String ?? String()

    }

}

Filter:

let arrayFiltered =  self.arrayUsers.filter { (user) -> Bool in
    return user.username.lowercased().contains("Dani".lowercased()) ||
           user.email.lowercased().contains("[email protected]".lowercased()) ||
           user.phoneNumber.lowercased().contains("+34111223344".lowercased())

}

Have a good day ♥️.

Comments

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.