Maybe my title is a little bit confusing so I’ll illustrate with a scenario
Let say I want to compare between 2 int values and return me a boolean based on the logical comparison.
if (int1 > int2) return true;
if (int3 < int4) return true;
if (int5 == int6) return true;
if (int7 >= int8) return true;
if (int9 <= int10) return true;
But instead of writing this manually, I wish to do something like
- (bool)compareVal1: (int)val1 withVal2: (int)val2 usingLogical: (NSString*)logic
{
if (val1 "logic" val2) return true;
}
I want a general function which can be used to represent the 5 logical comparison that I want.
I’m not sure whether this is achievable but if you have any other solutions, please advise 🙂
Well first off, I would use an enum instead of an NSString to decide which logical operator to use. So try something like this:
And the actual function would look like this:
I would also make the function a static/class function because it does not truly affect a specific instance of a class, but rather it is a utility function that operates on the values passed in. An example of this functions usage would be this: