Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Forked from pcperini/NSNumber+Ranges.m
Created July 22, 2012 02:31
Show Gist options
  • Save codeswimmer/3157991 to your computer and use it in GitHub Desktop.
Save codeswimmer/3157991 to your computer and use it in GitHub Desktop.
Quick Ranges from NSNumbers
// .h
@interface NSNumber (Ranges)
- (NSRange)to:(NSNumber *)rangeEnd;
- (BOOL)isInRange:(NSRange)range;
@end
// .m
@implementation NSNumber (Ranges)
- (NSRange)to:(NSNumber *)rangeEnd
{
return NSMakeRange([self intValue], [rangeEnd intValue] - [self intValue]);
}
- (BOOL)isInRange:(NSRange)range
{
return ([self intValue] >= range.location) && ([self intValue] <= (range.length + range.location));
}
@end
// .h
@interface NSNumber (Ranges)
- (NSRange)to:(NSNumber *)rangeEnd;
- (BOOL)isInRange:(NSRange)range;
@end
// .m
@implementation NSNumber (Ranges)
- (NSRange)to:(NSNumber *)rangeEnd
{
return NSMakeRange([self intValue], [rangeEnd intValue] - [self intValue]);
}
- (BOOL)isInRange:(NSRange)range
{
return ([self intValue] >= range.location) && ([self intValue] <= (range.length + range.location));
}
@end
// use
NSLog(@"%@", [@"pcperini" substringWithRange: [@2 to: @7]]);
// logs 'perini`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment