本文介绍了目标 C:NSRange 或类似的浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些方法,我希望它们返回一系列值(从:235 到:245).我使用 NSRange 作为返回值

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;

只要返回值是一个 integer 范围(例如位置:235,长度:10),这个方法就可以正常工作.

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

但现在我有一个问题,我需要返回一个 float 范围(例如位置:500.5,长度:0.4).我阅读了 doc NSRange 是带有 NSUIntegers 的 typedefed 结构.是否可以为 float 范围定义另一个结构?如果是这样,是否有类似于 NSMakeRange(NSUInteger loc, NSUInteger len) 的方法来创建那些 float 范围?

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?

推荐答案

尽管您可以重用图形库中的多个配对对象"之一(您可以从 CGPointCGSize 或它们的 NS... 等价物)这些对象背后的 struct 非常简单,最好创建自己的:

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

这篇关于目标 C:NSRange 或类似的浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:10