Skip to content

Instantly share code, notes, and snippets.

@kimhunter
Created May 29, 2014 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimhunter/4ef74fe6e8b50f843984 to your computer and use it in GitHub Desktop.
Save kimhunter/4ef74fe6e8b50f843984 to your computer and use it in GitHub Desktop.
Add FastEnumeration to NSNumber
/*
inspired by ruby's 4.times {} method, created becasue I can and it was fun to do.
Usage:
for (NSNumber *i in @100)
{
NSLog(@"%@", i);
}
*/
@interface NSNumber (FastEnum)<NSFastEnumeration>
@end
@implementation NSNumber (FastEnum)
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
{
NSUInteger count = 0;
if (state->state == 0)
{
// can't be nil but won't change
state->mutationsPtr = &state->extra[0];
state->extra[1] = labs([self longValue]);
state->extra[2] = [self longValue] < 0; // is negative
}
if (state->state < state->extra[1])
{
state->itemsPtr = buffer;
while((state->state < state->extra[1]) && (count < len))
{
long num = state->extra[2] ? -state->state : state->state;
buffer[count] = @(num);
state->state++;
count++;
}
}
return count;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment