0

I have an array of User objects with status as property in it.

Now i want to filter the objects using status it can either

Status: Waiting InProgress JustResearch NotInterested NotApplicable Intertested AlreadyBooked

one more property eligibility 10 20 30

let array = Array<User> // assuming this has array of user values

let filteredArray = array.filter { !$0.status.contains("Waiting") || !$0.status.contains("JustResearch") !$0.status.contains("AlreadyBooked") || !$0.eligibility.contains("20")}

I need something like above filteredArray should contain only user objects with InProgress, JustResearch,NotInterested and NotApplicable

I am getting an error while i am trying use filter as above how can apply filter with multiple conditions.

2
  • 1
    What is the error? Commented Aug 30, 2017 at 7:18
  • what do you meant by one more property eligibility 10 20 30 ? Commented Aug 30, 2017 at 7:21

3 Answers 3

0

You lost || after

!$0.status.contains("JustResearch")

This code will work:

let filteredArray = array.filter { !$0.status.contains("Waiting") || !$0.status.contains("JustResearch") || !$0.status.contains("AlreadyBooked")}
Sign up to request clarification or add additional context in comments.

Comments

0

Actually you don't want to check if status contains a string, you want to check if status is equal to a string.

A possible solution is to create an whiteList array. Then you can check if the array contains the status.

let array = Array<User> // assuming this has array of user values
let whiteList = ["InProgress", "JustResearch", "NotInterested", "NotApplicable"]
let filteredArray = array.filter { whiteList.contains($0.status) }

Comments

0

Use the following code.

i modified your condition (ie !$0.status.contains("Waiting") to $0.status.contains("InProgress")) because you need only user objects with InProgress, JustResearch,NotInterested and NotApplicable.

let filteredArray = array.filter { $0.status.contains("InProgress") || $0.status.contains("JustResearch") || $0.status.contains("NotInterested") || $0.status.contains("NotApplicable")}

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.