Reputation: 2485
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 ?
Upvotes: 3
Views: 687
Reputation: 5818
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.
Upvotes: 3
Reputation: 9721
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;
}
Upvotes: 3