I’m trying to use NSRange to hold a range of years, such as
NSRange years = NSMakeRange(2011, 5);
I know NSRange is used mostly for filtering, however I want to loop over the elements in the range. Is that possible without converting the NSRange into a NSArray?
It kind of sounds like you’re expecting
NSRangeto be like a Pythonrangeobject. It’s not;NSRangeis simply a structnot an object. Once you’ve created one, you can use its members in a plain old
forloop:(Still working on the assumption that you’re thinking about Python.) There’s syntax in ObjC called fast enumeration for iterating over the contents of an
NSArraythat is pleasantly similar to a Pythonforloop, but since literal and primitive numbers can’t be put into anNSArray, you can’t go directly from anNSRangeto a Cocoa array.A category could make that easier, though:
Then you can create an array and use fast enumeration: