简体   繁体   English

在swift中过滤数组

[英]filtering array in swift

How can i filter an array of custom objects by one ore more flags ? 如何通过一个或多个标志过滤自定义对象数组?

let flags = ["New product", "Season 2014", "Season 2015", "Product available"]

With one flag or more static flags is easy: 使用一个或多个标志或更多静态标志很容易:

let filteredArray = myCustomObjectsArray.filter() { $0.isNew == true }

let filteredArray = myCustomObjectsArray.filter() { $0.isNew == true && $0.season.rangeOfString("14") && $0.season.rangeOfString("15") && $0.isAvailable }

But what if flags are dynamic ie flags array is created by user tapping on cells of the tableview ? 但是如果标志是动态的,那么标志数组是由用户点击tableview的单元格创建的?

Other problem is an error when trying to concatenate multiple conditions in `filter() { condition1 && condition2 etc.}. 其他问题是在尝试连接`filter(){condition1 && condition2 etc.}中的多个条件时出错。 " Expression was to complex to be solved in reasonable time ...". “在合理的时间内表达是复杂的......”。

So, flags array is what user selected (just titles from a tableview cells). 因此,flags数组是用户选择的(只是来自tableview单元格的标题)。 If flags array is ["New product", "Season 2015"], i want to filter by .isNew and .season.rangeOfString("15") for example. 如果flags数组是[“新产品”,“第2015季”],我想过滤.isNew和.season.rangeOfString(“15”)。 So i'm sorting by properties and NOT by string. 所以我按属性排序而不是按字符串排序。

You have not posted all of the necessary code, where does .isNew and .season come from? 你没有发布所有必要的代码, .isNew.season来自哪里? It would appear to be custom objects. 它似乎是自定义对象。

The error you refer to ("Expression was too complex to be solved in reasonable time") already has an answer: 您所引用的错误(“表达式太复杂而无法在合理的时间内解决”)已经有了答案:

If condition failing with expression too complex 如果条件失败,表达式太复杂

Having said that, you should be able to resolve this by separating out each part of the expression into separate statements: 话虽如此,您应该能够通过将表达式的每个部分分离为单独的语句来解决此问题:

let filteredArray = myCustomObjectsArray.filter() {
    let isNew = $0.isNew == true
    let is14 = $0.season.rangeOfString("14")
    let is15 = $0.season.rangeOfString("15")
    let isAvailable = $0.isAvailable
    return isNew && is14 && is15 && isAvailable
}

For multiple condition try the following code. 对于多个条件,请尝试以下代码。 It might be helpful to you. 它可能对你有所帮助。

let tryAnchoredFirst = array1.filter { (text) -> Bool in
            let tmp: NSString = text

            var range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch )
            return range.location != NSNotFound
        }
        // If the result of the filter is zero for first case then it will go to else part
        if tryAnchoredFirst.count > 0 {
            self.filteredTableData = tryAnchoredFirst
        }
        else {

            // Check second array
            self.filteredTableData = array2.filter { (text) -> Bool in

                let tmp: NSString = text
                var range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
                return range.location != NSNotFound
            }
        }

Your Array is : 你的数组是:

var flags = ["New product", "Season 2014", "Season 2015", "Product available"]

First of All you declare filteredArray 首先,您声明filteredArray

var filteredArray : [String] = [String]()

Make one Method rangeString to Check String is Available or Not. 使一个方法rangeString检查字符串是否可用。

func rangeString(x : String) -> [String]
    {
        if x.rangeOfString("Season") != nil {

            filteredArray += [x]
        }
        return filteredArray
    }

Now Calling the function as below 现在调用函数如下

flags.map({x in self.rangeString(x)})

Print yout filteredArray and got result. 打印yout filteredArray并得到结果。

 println(filteredArray)

在此输入图像描述

Note : Apply same idea for two string checking in array. 注意:对数组中的两个字符串检查应用相同的想法。

Filter array 过滤数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM