Target C: NSRange or similar with a float?

For some methods, I want them to return a range of values โ€‹โ€‹(from: 235 to: 245). I did this using NSRange as 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 a problem that I need to return a range of float (e.g. location: 500.5, length: 0.4). I read in doc that NSRange is a typed structure with NSUIntegers . Is it possible to type another structure for float ranges? And if so, could there be a similar NSMakeRange(NSUInteger loc, NSUInteger len) method for creating these float ranges?

+4
source share
3 answers

Although you can reuse one of several โ€œpaired objectsโ€ from the graphics library (you can choose from CGPoint or CGSize or their NS... equivalents NS... ), the struct behind these objects is 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; } 
+7
source

Yes, you can even take a look at the definitions of NSRange and NSMakeRange (in NSRange.h , the easiest way to get the "click" NSRange on the NSRange name in Xcode) to see how to do this:

 typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len) { NSRange r; r.location = loc; r.length = len; return r; } 
+1
source

If you just need an existing structure with 2 floats and you don't mind including GLKit, you can use GLKVector2. It also has a Make function.

0
source

Source: https://habr.com/ru/post/1493469/


All Articles