Search code examples
objective-ctypedefnsrange

Objective C: NSRange or similar with float?


For some methods I want them to return a range of values (from: 235, to: 245). I did this using NSRange as a return value

- (NSRange)giveMeARangeForMyParameter:(NSString *)myParameter;

This works fine as long as the return value is an integer range (e.g. location: 235, length: 10).

But now I have the problem that I need to return a float range (e.g. location: 500.5, length: 0.4). I read in the doc that NSRange is a typedefed struct with NSUIntegers. Is it possible to typedef another struct for float ranges? And if so, can there be a similar method to NSMakeRange(NSUInteger loc, NSUInteger len) to create those float ranges?


Solution

  • Although you could reuse one of several "pair objects" from the graphics library (you can pick from a CGPoint or CGSize, or their NS... equivalents) the structs behind these objects are so simple that it would be better to create your own:

    typedef struct FPRange {
        float location;
        float length;
    } FPRange;
    
    FPRange FPRangeMake(float _location, float _length) {
        FPRange res;
        res.location = _location;
        res.length = _length;
        return res;
    }