views:

128

answers:

1

I am developing an app in iPhone

I am using an NSRange object in my code.

if (range.length == 0) { do something }

When I print the the value of range.length on the console it writes 0. Which means my if condition holds valid. But it never enter inside the loop to do something.

Any clue what I might be doing wrong?

Thanks AJ

+1  A: 

The NSRange is a struct defined like this:

typedef struct _NSRange {
    NSUInteger location;
    NSUInteger length;
} NSRange;

If (myRange.length == 0) is not true, the length of your range is not 0. The comparison is right, search elsewhere for your problem. Use the debugger to see exactly what's going on and when.

Nikolai Ruhe
For example, try doing an NSLog of the value immediately before the if statement, then again immediately inside the if statement. You could NSLog the value of (range.length == 0) as well to see if it's returning what you expect it to.
Ed Marty
Thanks for your response. The NSLog did print the correct value. But this is how I made it work. The return type of range.length is an NSUInteger. I had to type cast the range.length to NSInteger and it worked...went into the loop!Thanks for your response
AJ