Search code examples
iosobjective-cnsintegercgfloat

What is the equivalent of NSNotFound for floats


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 ?


Solution

  • 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;
    }