It looks like you expect NSRange be like a Python range object. Not this; NSRange is just a structure
typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange;
not an object. Once you have created it, you can use its elements in a simple old for chain:
NSUInteger year; for(year = years.location; year < NSMaxRange(years); year++ ){
(Still working on what you think of Python.) There, the syntax in ObjC is called a quick enumeration for repeating the contents of a NSArray , which nicely looks like a Python for loop, but since literals and primitive numbers cannot be placed in NSArray , you donβt can go directly from the NSRange array to Cocoa.
A category can make it easier:
@implementation NSArray (WSSRangeArray) + (id)WSSArrayWithNumbersInRange:(NSRange)range { NSMutableArray * arr = [NSMutableArray array]; NSUInteger i; for( i = range.location; i < NSMaxRange(range); i++ ){ [arr addObject:[NSNumber numberWithUnsignedInteger:i]]; } return arr; }
Then you can create an array and use a quick enumeration:
NSArray * years = [NSArray WSSArrayWithNumbersInRange:NSMakeRange(2011, 5)]; for( NSNumber * yearNum in years ){ NSUInteger year = [yearNum unsignedIntegerValue];
source share