Sitemap

Crash in NSArray literals @[obj,…]

1 min readNov 29, 2018

--

This is a follow-up of my previous article about crash in NSDictionary literals. When we create a NSArray using the literals, be aware not to include any nil objects, otherwise the app will crash.

Get John Chen’s stories in your inbox

Join Medium for free to get updates from this writer.

NSObject *obj = nil;
id a = @[.., obj]; // All objects inside the literals cannot be nil.

Just like the literals of NSDictionary, the array literals are translated into the following initialization function.

+ (instancetype)arrayWithObjects:(ObjectType  _Nonnull const *)objects count:(NSUInteger)cnt;

--

--