If filter returns nil, how do I not return empty result?

Seong Lee

I'm doing the following filtering that returns a recipe list, filtered by given category name value.

filteredRecipe = filteredRecipe.filter({
    if let category = $0.valueForKey("category") as? NSManagedObject {
        if let name = category.valueForKey("name") as? String {
            return name.rangeOfString(cap) != nil
        } else {
            return false
        }
    } else {
        return false
    }
})

This filter works in association with searchBar textfield so I will possibly have random value in the textfield which will make filteredRecipe to hold unreliable data.

I need to make sure when the filter can't find any recipe from filteredRecipe with given category name value, I leave filteredRecipe untouched. Currently, when there is no match, it makes filteredRecipe empty array []. I'm not sure what part causes this.

Paulw11

You need to assign the filtering result to a temporary variable and check that it isn't empty.

let filtered = filteredRecipe.filter({
    if let category = $0.valueForKey("category") as? NSManagedObject {
        if let name = category.valueForKey("name") as? String {
            return name.rangeOfString(cap) != nil
    } 
    return false
})

if !filtered.isEmpty {
    filteredRecipe = filtered
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

If filter returns nil, how do I not return empty result?

From Dev

How do I allow an empty result in my filter?

From Java

How do I stop iteration and return an error when Iterator::map returns a Result::Err?

From Java

In MVC, how do I return a string result?

From Dev

How do I return a result from a dialog?

From Dev

How do I return result from a spirngEventListener

From Dev

How can I filter an iterator when the predicate returns a Result<bool, _>?

From Dev

How can I filter an iterator when the predicate returns a Result<bool, _>?

From Dev

elasticsearch nested filter return empty result

From Dev

How do I raise an exception if an evaluation returns nil in Ruby?

From Dev

How do I handle nil return values from database?

From Dev

webservice returns SID - how do I store result as an integer?

From Dev

How do I filter an empty array that has a white space?

From Dev

How do i return a result from a async task

From Dev

How do I return the result of a RETURNING clause from a plpgsql function?

From Dev

How do I get the result or return value of a Task?

From Dev

How do i return all the rows of a result set to a client?

From Dev

How do I return the result of a RETURNING clause from a plpgsql function?

From Dev

Shapeless and gremlin scala: How do I return the result of a call to `as`?

From Dev

How do I return an empty array / array of length 0 in perl?

From Dev

How do I return an empty string from an RPC call?

From Dev

How do I return a Filter iterator from a function?

From Dev

How do I use .filter to return multiple values?

From Dev

How do I return a html code as output of filter in Angular js

From Dev

JsonResult - how to return an empty JSON result?

From Dev

How to return an empty result cursor on publication?

From Dev

How can I check if Play Slick filter has returned some rows or an empty result?

From Dev

How can I check if Play Slick filter has returned some rows or an empty result?

From Dev

Emacs Lisp - How do I handle an edge case where a function returns a string or nil?

Related Related

  1. 1

    If filter returns nil, how do I not return empty result?

  2. 2

    How do I allow an empty result in my filter?

  3. 3

    How do I stop iteration and return an error when Iterator::map returns a Result::Err?

  4. 4

    In MVC, how do I return a string result?

  5. 5

    How do I return a result from a dialog?

  6. 6

    How do I return result from a spirngEventListener

  7. 7

    How can I filter an iterator when the predicate returns a Result<bool, _>?

  8. 8

    How can I filter an iterator when the predicate returns a Result<bool, _>?

  9. 9

    elasticsearch nested filter return empty result

  10. 10

    How do I raise an exception if an evaluation returns nil in Ruby?

  11. 11

    How do I handle nil return values from database?

  12. 12

    webservice returns SID - how do I store result as an integer?

  13. 13

    How do I filter an empty array that has a white space?

  14. 14

    How do i return a result from a async task

  15. 15

    How do I return the result of a RETURNING clause from a plpgsql function?

  16. 16

    How do I get the result or return value of a Task?

  17. 17

    How do i return all the rows of a result set to a client?

  18. 18

    How do I return the result of a RETURNING clause from a plpgsql function?

  19. 19

    Shapeless and gremlin scala: How do I return the result of a call to `as`?

  20. 20

    How do I return an empty array / array of length 0 in perl?

  21. 21

    How do I return an empty string from an RPC call?

  22. 22

    How do I return a Filter iterator from a function?

  23. 23

    How do I use .filter to return multiple values?

  24. 24

    How do I return a html code as output of filter in Angular js

  25. 25

    JsonResult - how to return an empty JSON result?

  26. 26

    How to return an empty result cursor on publication?

  27. 27

    How can I check if Play Slick filter has returned some rows or an empty result?

  28. 28

    How can I check if Play Slick filter has returned some rows or an empty result?

  29. 29

    Emacs Lisp - How do I handle an edge case where a function returns a string or nil?

HotTag

Archive