NSArray out of bounds check

spacebiker picture spacebiker · Mar 15, 2012 · Viewed 11.8k times · Source

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

Answer

sch picture sch · Mar 15, 2012
if (array.count > 4) {
    sections = [array objectAtIndex:4];
}