PDA

View Full Version : NSRange




varsis
Sep 15, 2008, 10:58 AM
Here is what I am trying to do,

I retrieve two NSRanges one which comes from a document and one that is in the application I want to know if the Ranges are overlapping in anyway if they are. Also get the portion of the overlapping range.



robbieduncan
Sep 15, 2008, 11:16 AM
NSRange is just a C struct as described in this document (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html). You can simply retrieve the location and length from each range and work out if they overlap.

There is also the very handy NSIntersectionRange (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSIntersectionRange) function which will return a new range representing the intersection (or overlap). This basically does exactly what you want.

varsis
Sep 15, 2008, 11:24 AM
NSRange is just a C struct as described in this document (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html). You can simply retrieve the location and length from each range and work out if they overlap.

There is also the very handy NSIntersectionRange (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSIntersectionRange) function which will return a new range representing the intersection (or overlap). This basically does exactly what you want.

Thanks! Got it working...