Create a gist now

Instantly share code, notes, and snippets.

Re-declaration of existing `NSRange` functions and implementation of new functions to match conventions of comparable Foundation and Core Foundation types.
NS_INLINE NSRange NSRangeMake(NSUInteger loc, NSUInteger len) {
return NSMakeRange(loc, len);
NS_INLINE NSUInteger NSRangeMax(NSRange range) {
return NSMaxRange(range);
NS_INLINE BOOL NSRangeContainsLocation(NSUInteger loc, NSRange range) {
return NSLocationInRange(loc, range);
NS_INLINE BOOL NSRangeEqualToRange(NSRange range1, NSRange range2) {
return NSEqualRanges(range1, range2);
NS_INLINE NSRange NSRangeZero() {
return NSRangeMake(0, 0);
NS_INLINE NSRange NSRangeUnion(NSRange range1, NSRange range2) {
return NSUnionRange(range1, range2);
NS_INLINE NSRange NSRangeIntersection(NSRange range1, NSRange range2) {
return NSIntersectionRange(range1, range2);
NS_INLINE NSDictionary * NSRangeCreateDictionaryRepresentation(NSRange range) {
return @{@"location": @(range.location), @"length": @(range.length)};
NS_INLINE BOOL NSRangeMakeFromDictionaryRepresentation(NSDictionary *representation, NSRangePointer range) {
if (!representation[@"location"] || !representation[@"length"]) {
return NO;
NSRange r;
r.location = [representation[@"location"] unsignedIntegerValue];
r.length = [representation[@"length"] unsignedIntegerValue];
range = &r;
return YES;
NS_INLINE NSRange NSRangeZero() {
    NSRange range;
    return range;

Isn't that NSRange uninitialized?


You're right. It should be defined as a constant instead, like CGRectZero, etc.

typedef NSRangeZero (NSRange){0, 0}


typedef NSRangeZero NSRangeMake(0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment