objective c compare range intersect

You can use this method for that purpose:

NSRange NSIntersectionRange (
   NSRange range1,
   NSRange range2
);

You can find all the info here:

NSIntersectionRange Apple Doc


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

Tags:

Objective C