Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSArray difference between firstObject and objectAtIndex:0 [duplicate]

In the following example array objectAtIndex:0 crashes but firstObject resulting a nil value. Why this happening?

NSArray *array = [NSArray new];
id obj1 = [array firstObject]; // this is giving nil value
id obj2 = [array objectAtIndex:0]; // this line crashes
like image 364
Jobin Jose Avatar asked Dec 02 '22 13:12

Jobin Jose


1 Answers

Using firstObject is a system defined API which returns nil if there is none. But if you try to access objectAtIndex it will give NSRange exception and app will crash.

like image 194
Paul Subhajit Avatar answered Dec 05 '22 08:12

Paul Subhajit