I have searched quite bit on stackoverflow and other resources, can't find an answer.
What is the difference between the NSArray methods objectAtIndexedSubscript
and objectAtIndex
?
Or, if there is no difference, why do both exist?
In the Apple Docs it says they are "identical".
I am super new to programming but if they are identical, isn't that breaking the DRY principle? Obviously the guys at Apple know what they are doing, but why are they both provided if they are identical? Or maybe a better question is, why should I use one over the other?
They are identical.
-objectAtIndex:
is the method you should use.
-objectAtIndexedSubscript:
is the method that provides support for obj-c subscripts. In other words, this method is what the compiler uses if you say array[3]
. But for NSArray*
it does the exact same thing as -objectAtIndex:
. The reason why it's a different method is so other classes can implement this in order to support obj-c subscripts without having to use the generically-named -objectAtIndex:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With