Is there any way to enforce typing on NSArray, NSMutableArray, etc.?

Sam Lee picture Sam Lee · Mar 16, 2009 · Viewed 40.6k times · Source

Can I make a NSMutableArray instance where all the elements are of type SomeClass?

Answer

Logan picture Logan · Jun 25, 2015

Nobody's put this up here yet, so I'll do it!

Tthis is now officially supported in Objective-C. As of Xcode 7, you can use the following syntax:

NSArray<MyClass *> *myArray = @[[MyClass new], [MyClass new]];

Note

It's important to note that these are compiler warnings only and you can technically still insert any object into your array. There are scripts available that turn all warnings into errors which would prevent building.