Using Generics

suggest change

For added safety we can define the type of object that the array contains:

NSArray<NSString *> *colors = @[@"Red", @"Green", @"Blue", @"Yellow"];
NSMutableArray<NSString *> *myColors = [NSMutableArray arrayWithArray:colors];
[myColors addObject:@"Orange"]; // OK
[myColors addObject:[UIColor purpleColor]]; // "Incompatible pointer type" warning

This check happens only at compile time. At run time the array is still an ordinary NSMutableArray.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents