Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between objectAtIndexedSubscript and objectAtIndex

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?

like image 801
Joshua Dance Avatar asked Jan 30 '13 20:01

Joshua Dance


1 Answers

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:.

like image 84
Lily Ballard Avatar answered Nov 08 '22 23:11

Lily Ballard