封闭间隔有助于Swift

时间:2016-02-20 18:11:29

标签: swift contains intervals

我想比较2个不同的封闭间隔,以检查它们是否有任何重叠

var a = 1.0 ... 5.0

var x = 2.5 ... 6.5
var y = 0.5 ... 7.0
var z = 5.5 ... 8.0

compare(a,x) //should return true
compare(a,y) //should return true
compare(a,z) //should return false

swift有内置的任何功能吗?我所能找到的只是间隔的包含方法,但是当我这样做时我得到error

1 个答案:

答案 0 :(得分:2)

检查overlaps方法

a.overlaps(x) // return true
a.overlaps(y) // return true
a.overlaps(z) // return false