What is the equivalent of NSNotFound for floats

689 Views Asked by At

What if I have a method that returns a CGFloat and that method could not find an expected number, I would like to return something like NSNotFound, but that is an NSInteger.

Whats the best practice for this ?

2

There are 2 best solutions below

4
On BEST ANSWER

You could use not a number (NaN).

See nan(), nanf() and isnan().

However for these issues, where there is no clearly defined non-value (it's worse with integers), then 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;
}
5
On

A pretty clean way is to wrap it into an 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-existing value.