About NSMutableArray objectAtIndex

Is objectAtIndex starting with 1 instead of 0? I use objectAtIndex and pass the data 0, but he said that it is not connected. So, I think it starts with 1, am I right?

+4
source share
3 answers

NSMutableArray is indexed by zero. However; from docs :

If the index is outside the array (that is, if the index is greater than or equal to the value returned by the counter), an NSRangeException is thrown.

If nothing is stored in the array, access to index 0 will cause the above test to fail and cause an error.

+4
source

It starts at 0. If you get an exception, it is because the array was empty. You must first add something to the array before it has any content. Use addObject: to put elements into it. Use count to find out how many items are currently in it.

+5
source

No, it starts at 0.

Make sure your array is full. If the array is empty, you cannot use -objectAtIndex: with any indexes, since there is no way to return something.

+4
source

Source: https://habr.com/ru/post/1306922/


All Articles