Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8589837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:09:08+00:00 2026-06-11T23:09:08+00:00

Maybe my title is a little bit confusing so I’ll illustrate with a scenario

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:09:09+00:00Added an answer on June 11, 2026 at 11:09 pm

    Well first off, I would use an enum instead of an NSString to decide which logical operator to use. So try something like this:

    typedef enum {
    
        GREATER_THAN,
        LESS_THAN,
        LESS_THAN_OR_EQUAL,
        GREATER_THAN_OR_EQUAL,
        EQUALS
    
    } logicalOperator;    
    

    And the actual function would look like this:

    + (bool)compareVal1: (int)val1 withVal2: (int)val2 usingLogical: (logicalOperator)op
    {
        switch(op) {
    
            case GREATER_THAN:
                return (val1 > val2);
    
            case LESS_THAN:
                return (val1 < val2);
    
            case LESS_THAN_OR_EQUAL:
                return (val1 <= val2);
    
            case GREATER_THAN_OR_EQUAL:
                return (val1 >= val2);
    
            case EQUALS:
                return (val1 == val2);
        }
    }
    

    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:

    bool isGreater = [MyClass compareVal1: 4 withVal2: 3 usingLogical: GREATER_THAN];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe the title is a little bit obscure, but let me explain... In a
Ok the title maybe a little confusing. I have a database with the table
I want to do THIS , just a little bit more complicated: Lets say,
Maybe the question title is a little confusing, so I'll try to clarify things
The title maybe a little vague, let me give you an example: if we
Ok, maybe the title is a little confusing, but what I am trying to
Maybe the title is a little missleading but I'd like to ask you for
The title may be a little confusing, but I don't know how else to
Maybe my title is not the best but here is what I really want.I
Ok so my title may be confusing. What I want to do is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.