对象数组过滤多个验证

时间:2017-08-30 07:10:13

标签: ios arrays swift

我有一个User对象数组,其中包含status属性。

现在我想用状态过滤对象

状态: 等候 进行中 JustResearch 没兴趣 不适用 Intertested AlreadyBooked

还有一项财产资格 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")}

我需要类似上面的内容filteredArray应该只包含InProgress,JustResearch,NotInterested和NotApplicable的用户对象

我在尝试使用过滤器时出现错误,如何应用具有多个条件的过滤器。

3 个答案:

答案 0 :(得分:0)

使用以下代码。

我修改了你的条件(即!$0.status.contains("Waiting")$0.status.contains("InProgress")),因为你只需要有InProgress,JustResearch,NotInterested和NotApplicable的用户对象。

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

答案 1 :(得分:0)

之后丢失了SELECT app_user.au_userid, (SELECT COUNT (app_user.au_id) FROM app_user JOIN orders ON app_user.au_id = orders.o_au_id GROUP BY app_user.au_id) AS total, SUM (order_detail.od_quantity), (order_detail.od_quantity*SUM (order_detail.od_subtotal)) AS total_amount FROM app_user JOIN orders ON app_user.au_id = orders.o_au_id JOIN order_detail ON orders.o_id = order_detail.od_order_id GROUP BY app_user.au_userid
||

此代码可以使用:

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

答案 2 :(得分:0)

实际上您不想检查status 是否包含字符串,您想检查字符串status 是否相等

可能的解决方案是创建 whiteList 数组。然后,您可以检查数组是否包含状态。

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) }
相关问题