strawnut strawnut - 6 months ago 17
iOS Question

Can I use '>=' for NSComparisonResult?

I want to check that NSComparisonResult is less than or equal in

iOS
.

1.

NSComparisonResult result = blabla...;
if (result == NSOrderedAscending || result == NSOrderedSame) {...}


2.

NSComparisonResult result = blabla...;
if (result <= NSOrderedSame) {...}


I can use option 1, but, can I use option 2? I designed it because the
NSComparisonResult
is just
NSInteger
;
NSOrderedSame
is 0,
NSOrderedAscending
is -1,
NSOrderedDescending
is 1.

Answer

Option 2 may work if you are careful. It's not clear what <= means versus >= so it is confusing. And it's far from ideal to make any assumptions about the enum values.

Option 3 is a better alternative:

NSComparisonResult result = ...
if (result != NSOrderedDescending) {
    // It's either ascending or same
}