Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

nsrange

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.

like image 596
Aashish Nagar Avatar asked Sep 15 '25 19:09

Aashish Nagar


1 Answers

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

NSIntersectionRange(range1, range2).length > 0
like image 106
Gerriet Avatar answered Sep 18 '25 08:09

Gerriet