目标 c 比较范围相交 [英] objective c compare range intersect

查看:67
本文介绍了目标 c 比较范围相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到 2 个数字范围的交集,例如……

I am trying to find the intersection of 2 number ranges, say for example...

范围 A 是从 10 到 100,范围 B 是从 60 到 70

range A is from 10 to 100, range B is from 60 to 70

是否有一种简单的方法无需编写大量 if 语句来计算两个范围的交集,因此在此示例中为 10.

Is there an easy way without writing a load of if statements to calculate the intersection of the two ranges, so in this example it would be 10.

谢谢,

推荐答案

如果您拥有或制作 NSRange 对象,NSIntersectionRange 函数将为您完成此操作.当没有交集时,请务必检查它返回的内容.

If you have or make NSRange objects, the NSIntersectionRange function will do this for you. Just be sure to check what it returns when there is no intersection.

NSRange a = NSMakeRange(10, 90);
NSRange b = NSMakeRange(60, 10);
NSRange intersection = NSIntersectionRange(a, b);
if (intersection.length <= 0)
    NSLog(@"Ranges do not intersect");
else
    NSLog(@"Intersection = %@", NSStringFromRange(intersection));

这篇关于目标 c 比较范围相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆