简体   繁体   中英

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 ?

Other problem is an error when trying to concatenate multiple conditions in `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). If flags array is ["New product", "Season 2015"], i want to filter by .isNew and .season.rangeOfString("15") for example. 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? 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

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

Make one Method rangeString to Check String is Available or Not.

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.

 println(filteredArray)

在此输入图像描述

Note : Apply same idea for two string checking in array.

Filter array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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