Search code examples
cocoansmutablearraynsarrayindexoutofboundsexception

NSArray out of bounds check


noobie question.. What is the best way to check if the index of an NSArray or NSMutableArray exists. I search everywhere to no avail!!

This is what I have tried:

if (sections = [arr objectAtIndex:4])
{
    /*.....*/
}

or

sections = [arr objectAtIndex:4]
if (sections == nil)
{
    /*.....*/
}

but both throws an "out of bounds" error not allowing me to continue

(do not reply with a try catch because thats not a solution for me)

Thanks in advance


Solution

  • if (array.count > 4) {
        sections = [array objectAtIndex:4];
    }