What is the NSNotFound Equivalent for Floats

What if I have a method that returns CGFloat and that method couldn't find the expected number, I would like to return something like NSNotFound, but it's NSInteger.

What's the best practice for this?

+3


source to share


2 answers


You can use non-number ( NaN

).

See nan()

, nanf()

and isnan()

.



However, for these problems, when there is no clear obscene value (which is worse with integers), I prefer to use the following method semantics:

- (BOOL)parseString:(NSString *)string
            toFloat:(CGFloat *)value
{
    // parse string here
    if (parsed_string_ok) {
        if (value)
            *value = parsedValue;
        return YES;
    }
    return NO;
}

      

+3


source


A pretty simple way is to wrap it in NSNumber:

- (NSNumber *)aFloatValueProbably
{
    CGFloat value = 0.0;
    if (... value could be found ...) {
        return @(value);
    }
    return nil;
}

      



Then you can check if the function returned nil for your non-existent value.

+3


source







All Articles