Filter array in fast

How can I filter an array of custom objects with one or more flags?

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

      

With one flag or more static flags, it's 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 the flags are dynamic, i.e. is the flags array created by the user clicking on the table cells?

Another problem is an error when trying to combine multiple conditions in `filter () {condition1 && state2, etc.}. "The expression was difficult to resolve within a reasonable time ..."

So, the flags array is selected by the user (only headers from table cells). If the flags array is ["New Product", "Season 2015"], I want, for example, to filter with .isNew and .season.rangeOfString ("15"). So I am sorting by properties and NOT by row.

+3


source to share


3 answers


You haven't posted all the necessary code, where do the .isNew

and come from .season

? These are seemingly custom objects.

The error you are referring to ("The expression was too complex to solve in a reasonable time") already has an answer:

If the error condition is too complex



Having said that, you should be able to solve this problem by splitting each part of the expression into separate instructions:

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
}

      

+4


source


For multiple conditions, try the following code. This might be helpful for 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
            }
        }

      

0


source


Your array:

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 whether a string is available or not.

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

            filteredArray += [x]
        }
        return filteredArray
    }

      

Now the function call is below

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

      

Print the file filterArray and get the result.

 println(filteredArray)

      

enter image description here

Note. Apply the same idea to validate a string in an array.

Filter scale

0


source







All Articles