Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About NSMutableArray objectAtIndex

Is the objectAtIndex start with 1 instead of 0? I use the objectAtIndex , and passing data 0, but it said that this is out of bound. So, I guess that is start at 1, am I right?

like image 327
Tattat Avatar asked Dec 13 '22 22:12

Tattat


2 Answers

It starts at 0. If you got an exception, it's because the array was empty. You have to add something to the array first before it has any contents. Use addObject: to put items into it. Use count to find out how many items are currently in it.

like image 144
Ben Zotto Avatar answered Dec 15 '22 12:12

Ben Zotto


NSMutableArray is zero-indexed. However; from the docs:

If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count), an NSRangeException is raised.

If there is nothing stored in the array, accessing index 0 will fail the above test, and cause the error.

like image 36
e.James Avatar answered Dec 15 '22 13:12

e.James