Aashish Nagar
Aashish Nagar

Reputation: 1227

Check if one NSRange is within another NSRange or interSecting with it in swift ?

if case 1 : range1 is (5,10) and range2 is (8,0) it should return true. case 2 : range1 is (5,10) and range2 is (5,4) it should return true. case 3 if range1 is (5,10) and range2 is (14,20) it should return true. How to implement a function that return true for all cases. I want to check if a range intersect with another range or completely within it.

Upvotes: 4

Views: 3256

Answers (1)

Gerriet
Gerriet

Reputation: 1350

Range (the Swift way) has a method overlaps. If you want to work with NSRange it would be:

NSIntersectionRange(range1, range2).length > 0

Upvotes: 11

Related Questions