NSRange
NSRange is one of the essential types of Foundation. Passed around and returned in methods throughout the framework, being well-versed in this struct has a range of benefits, which this week’s article will help you locate. Ranges are data types used to describe a contiguous interval of integers. They are most commonly used with strings, arrays, and similarly-ordered collections.
NSRange is a Foundation struct for a contiguous interval of integers, with location (start) and length. It helps avoid off-by-one errors compared to start/end pairs.
Common uses:
- Strings:
rangeOfString:finds substrings;substringWithRange:extracts them. - Arrays:
subarrayWithRange:gets slices. - Check containment with NSLocationInRange.
Functions:
- NSMakeRange(loc, len) creates one.
- NSMaxRange(range) gives end (location + length).
- NSEqualRanges, NSIntersectionRange, NSUnionRange for comparisons and sets.
- NSStringFromRange, NSRangeFromString for string conversion.
Wrap in NSValue for collections. NSRangePointer is a typedef for NSRange *. CFRange is Core Foundation's version with CFIndex and CFRangeMake.
Category:
Tags:
Year: