Warning about using logical && between constant in

2019-09-07 05:45发布

问题:

I use this code to compare two strings:

NSComparisonResult result = [cellTitle compare:searchText options:(NSCaseInsensitiveSearch && NSLiteralSearch)];

but xcode shows warning about using logical && and offers to use bitwise &, by bitwise & the comparison don't return correct result.

How can I fix it?

回答1:

You're trying to combine two parts of a bitmask. You can use the bitwise OR or XOR operators, or an addition for this - |, ^ or +. The norm would be |.



回答2:

You should be doing:

 NSComparisonResult result = [cellTitle compare:searchText 
                            options:(NSCaseInsensitiveSearch | NSLiteralSearch)];

Those flags are a binary numbers with only one 1 set. So when you do a logical |, you get the "mix" of both (Ex: 1000 | 0100 => 1100), that way you can set several flags as options.