Search code examples
xcodeswiftnsstringnsrange

cannot invoke '!=' with an argument list of type (@lvalue Bool, ($T5))


The title is the error message that I get while compiling this code snippet @ line3:

var categoryMatch = (scope == "All") || (candy.category == scope)
var stringMatch = candy.name.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil);

Whats the problem here?

Thanks


Solution

  • The issue is rangeOfString: returns a NSRange that cannot be equatable to nil. Instead of that check the location property of NSRange.

    You need to change your return statement like:

    return categoryMatch && (stringMatch.location != NSNotFound)
    

    References: