Why does fast enumeration not skip the NSNumbers when I specify NSStrings?

前端 未结 8 1115
慢半拍i
慢半拍i 2021-01-14 02:27

I thought that I knew how to use fast enumeration, but there is something I don\'t understand about it. If I create three NSString objects and three NSNum

相关标签:
8条回答
  • 2021-01-14 02:47

    Objective-C is dynamically typed, meaning that at runtime (when the loop actually runs), objects are all effectively one type (id) with different classes. The language allows optional compile-time static typing, but all that does is check whether the messages you're sending are valid for the type you've marked. It doesn't actually change the behavior of your program. If you cast an object to be a different type than it actually is, all you're doing is lying to the compiler and defeating its type-checker.

    0 讨论(0)
  • 2021-01-14 02:48

    The for all loop doesn't know the difference between NSStrings and Integers -- it will simply go through the entire array, cast each as an NSString, and print them out as you asked.

    0 讨论(0)
提交回复
热议问题