-3

Given an array

NSArray *myArray = @[@"hello", @5, @[@6, @7, @8]];
for (NSNumber *myNumber in myArray)
{
   NSLog (@"%@", myNumber);
}

Would the output of the log message be 5 or 5 6 7 8. ie. Does the for/in loop introspect top level objects in a dictionary/array or deeply inspect nested sets, arrays, dictionaries, etc..?

Edit

I realize I'm taking a lot of heat for this question so let me clarify what I was getting at. Yes you are all right, for-in fast enumeration does not perform introspection, but why not? If I'm specifically designating a type such as NSNumber or NSString, why doesn't the for in loop perform a kindOfClass method with the type you designated rather than casting an object as the incorrect type? In the example above, is it not intuitive that I would only be iterating over the NSNumbers in the array?

I suspect that the answer is performing introspection on each item in an array may negate the performance enhancements of fast enumeration? But could this not be solved with using id when we want dynamic typing and explicit types when we'd like to perform introspection?

altyus
  • 606
  • 4
  • 13
  • 4
    What happened when you tried it? – jscs Feb 11 '14 at 04:08
  • Suggestion: take the time to study and understand iteration/enumeration (this is not introspection). As Josh comments: try it and also enough different configurations that you fully understand the for-in loop and arrays with array elements. – zaph Feb 11 '14 at 04:14
  • How can anyone be so lazy as to ask "what _would_ happen _if_ I ran this code"???? – matt Feb 11 '14 at 04:23
  • 3
    This question appears to be off-topic because it is so trivial as not be worth even a pennyworth of bandwidth. – matt Feb 11 '14 at 04:24
  • @matt Step away from SO, you are getting jaded, BTDT. – zaph Feb 11 '14 at 04:34
  • @Zaph if you understood the problem you'd clearly get the fact that a for in loop performs introspection. To be so rude and not understand such a simple concept leaves me a bit baffled. The reason I posted the question was simply because it seems like a reasonable one to ask and I couldn't find it posted elsewhere on Stack Overflow. Obviously If I can construct the code above, I can execute it in my simulator and read the result, but why not have it somewhere the people can Google and SO search? – altyus Feb 11 '14 at 04:44
  • @matt it's not a matter of lazy it's the fact that I couldn't find a repeat of the question, it seemed reasonable to ask, and to be accessible to others. So really it's the opposite of lazy because I could see a beginner programmer wondering if introspection in a for in loop reaches inside of arrays.. – altyus Feb 11 '14 at 04:46
  • @altyus Sorry but I am a little jaded myself with many of the questions that do not show much effort. I have spent the time studying and experimenting, that is how I learned. I have done what I suggested, written examples and tried them to understand how things work. I do that for many SO answers, I have a bunch of different projects just for that. Learning is best for me when I try things, failures and successes, much better than just asking and reading. FWIW, I have not down-voted your question but did add a close vote. – zaph Feb 11 '14 at 04:52
  • «if you understood the problem you'd clearly get the fact that a for in loop performs introspection.» Altyus, I'm quite confident that Zaph, matt, and I all understand exactly what's going on in your code snippet, and there _is no introspection_ here. You may be thinking of the term "iteration". – jscs Feb 11 '14 at 20:25

1 Answers1

3

Your code will generate:

 "hello"
 5
 (6, 7, 8)

A few comments...

1) The for loop only enumerates one level of whatever object you're enumerating, so it doesn't go down levels.

2) You should be careful to make sure that your loop variable is the correct type. Not every element of your NSArray is an NSNumber. If you try to send a message specific to NSNumber, you'll get a run-time error.

3) Your code doesn't crash because using %@ with NSLog sends a description message to the object, which is implemented by all the elements of the array. In your example, you should still make myNumber of type id, however, unless you're 100% sure everything in the array is actually an NSNumber.

godel9
  • 7,340
  • 1
  • 33
  • 53