Is there an easy way to iterate over an NSArray backwards?

前端 未结 5 1667
半阙折子戏
半阙折子戏 2020-12-29 01:57

I\'ve got an NSArray and have to iterate over it in a special case backwards, so that I first look at the last element. It\'s for performance reasons: If the la

5条回答
  •  星月不相逢
    2020-12-29 02:45

    To add on the other answers, you can use -[NSArray reverseObjectEnumerator] in combination with the fast enumeration feature in Objective-C 2.0 (available in Leopard, iPhone):

    for (id someObject in [myArray reverseObjectEnumerator])
    {
        // print some info
        NSLog([someObject description]);
    }
    

    Source with some more info: http://cocoawithlove.com/2008/05/fast-enumeration-clarifications.html

提交回复
热议问题