Skip to content

Instantly share code, notes, and snippets.

Last active January 22, 2020 23:32
Show Gist options
  • Save mattt/8402537 to your computer and use it in GitHub Desktop.
Save mattt/8402537 to your computer and use it in GitHub Desktop.
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;
Copy link

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

Copy link

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