views:

135

answers:

2

Okay here's the damned thing:

- (void)setMinimumNumberOfSides:(NSNumber *)newMinimumNumberOfSides {
    if (newMinimumNumberOfSides != minimumNumberOfSides) { 
        NSNumber *minimum = [[NSNumber alloc] initWithInt:(int)2];

        if (newMinimumNumberOfSides > minimum) {
            [newMinimumNumberOfSides retain];
            [minimumNumberOfSides release];
            minimumNumberOfSides = newMinimumNumberOfSides;
        } else {
            NSLog(@"setMinimumNumberOfSides: Invalid number of sides: %@ is smaller than the minimum of %@ allowed.",
            newMinimumNumberOfSides, minimum);
        }

        [minimum release];
        [newMinimumNumberOfSides release];
    }
}

There's something weird going on in there! The problem is my if (newMinimumNumberOfSides > minimum) {} condition. Even if newMinimumNumberOfSides is greated than minimum it goes into the else statement.

I did:

NSNumber *minimum = [[NSNumber alloc] initWithInt:(int)6];
[polygon setMinimumNumberOfSides:minimum];

which is way greater than 2. And I receive my error message...

I tried to NSLog those two, and it gives me the right numbers... So what's going on with this?

Thanks a lot!

+4  A: 

I think you need to change your if statement to:

if ([newMinimumNumberOfSides intValue] > [minimum intValue])

NSNumber is an object, so you have to get its integer value before you can start using it in comparisons.

darren
That is VERY logic, thanks a lot!
Tom
+1  A: 

You need to use the following method to compare NSNumber objects:

- (NSComparisonResult)compare:(NSNumber *)aNumber

but better yet just convert them to c ints: [myNSNumberValue inValue]

ennuikiller