Created
May 29, 2014 23:21
-
-
Save kimhunter/4ef74fe6e8b50f843984 to your computer and use it in GitHub Desktop.
Add FastEnumeration to NSNumber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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