I need to compare two numbers in a cocoa application, but it doesn't allow me to use the regular signs (>=, <=, <, >) nor will it let me use isGreaterThanOrEqualTo:[] (Which, I undestand only takes an object, but I thought I would try it). So can someone please tell me how you accomplish this? Thank you in advance.

Dani AI

Generated

Objective answer summary: the usual issue is mixing Objective‑C number objects (NSNumber ) with C comparison operators. The C operators (>, <, >=, <=) work on primitive types (int, float, double). If your variables are `NSNumber ` you must either extract a scalar or use NSNumber's comparison methods; comparing object pointers (or using operators inside message brackets) will give wrong results or compiler complaints.

Common, correct approaches:

  • Use NSNumber's -compare: (returns NSOrderedAscending, NSOrderedSame, NSOrderedDescending) or -isEqualToNumber::
NSNumber *a = @5;
NSNumber *b = @10;
NSComparisonResult r = [a compare:b];
if (r == NSOrderedAscending) {
    // a < b
} else if (r == NSOrderedSame) {
    // a == b
} else {
    // a > b
}
  • Convert to primitives and use C operators when appropriate:
if ([a doubleValue] >= [b doubleValue]) {
    // numeric comparison of values
}

Notes and pitfalls

  • == between NSNumber * compares pointers (object identity), not numeric equality; use -isEqualToNumber: or -compare: for value equality.
  • Foundation’s NSNumber does not declare -isGreaterThanOrEqualTo:; that name might appear in other APIs or custom categories, so calling it without a matching declaration will not compile.
  • For integers on 64‑bit machines prefer -integerValue or -longLongValue to avoid overflow; for precise decimal/currency math use NSDecimalNumber rather than double.
  • Floating point equality should use an epsilon test (avoid == for doubles).
  • If values come from UI controls (strings), parse them with NSNumberFormatter or -doubleValue/-integerValue depending on locale and precision needs.

As 's control flow shows, the structure works fine for scalars; was correct to flag NSNumber behavior. Common syntax errors arise from using operator syntax inside Objective‑C message brackets (for example writing something like [num1 >= num2]) or from mismatched types—check the variable declarations and use one of the patterns above.

Recommended Answers

All 4 Replies

Have you tried something like this:

if (num1 == num2) {
    //Do something.
}
else if (num1 == num3) {
    //Do another thing.
}
else if (num2 == num3) {
    //Do another thing.
}
else {
    //Do a third thing.
}

To be honest I have never really worked with Cocoa but I have a hard time believing that something like what I've done above is not possible.

Have you tried something like this:

// code snippet was here

To be honest I have never really worked with Cocoa but I have a hard time believing that something like what I've done above is not possible.

I know. It's quite odd, but I have tried just plainly entering in the symbols, but I get syntax errors everytime.It's something rather simple that I just haven't stumbled across yet.

Can you post the error messages and the relevant code?

Objective C is a bit tricky with numbers, you can mix it with C but the NSNumbers are tricky. Check out the NSNumbers and operators, you have to assign the values of the NSNumber to a variable and then you cand use this aritmethic operations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.