5

If I have the following piece of code:

        for (UIView *view in [myArray copy])

Does that mean that I'm sending the copy message to myArray on every iteration, or Objective-C is smart enough to do copy only once, and use it for the whole loop?

Devfly
  • 2,495
  • 5
  • 38
  • 56
  • 3
    Not hard to explain at all. You asked this question very clearly! – matt May 08 '13 at 20:50
  • 2
    The answer is that `copy` is only executed once. The expression on the right is evaluated before the `in` operation is initiated. – Hot Licks May 08 '13 at 20:51
  • Compare http://stackoverflow.com/questions/3882254/objective-c-for-each-fast-enumeration-evaluation-of-collection or http://stackoverflow.com/questions/12281461/is-this-an-inefficient-way-of-using-fast-enumeration. – Martin R May 08 '13 at 21:01

1 Answers1

5

It isn't a matter of "smart", but yes, the expression [myArray copy] is evaluated once up front.

matt
  • 515,959
  • 87
  • 875
  • 1,141