Are we able to specify a type on a NSArray * now?
Yes, through Objective-C's new lightweight generics. In the example you provided, you have a property of type NSArray
, which will accept elements that are UIView
s.
Now, this can be specified as follows (without __kindof
).
@property(nonatomic,readonly,copy) NSArray<UIView *> *arrangedSubviews;
And in this case, the array will accept objects whose class is UIView
, but not any objects which are subclasses of UIView
. The __kindof
declaration marks the array's generic type as one which can accept both instances of the UIView
class, and instances of any of UIView
's subclasses.
Edit:
I've removed the bulk of my original answer since I mistakenly thought specifying the type of the array would prevent you from inserting objects of an incorrect type, and this is not the case. (Thanks to Artem Abramov for pointing this out. Please see his answer below for additional details)
Objective-C's generics seem to exist to give you type information when accessing the elements of a generic collection. For example, consider the following code which adds a UIView
and a UIImageView
to NSMutableArray<UIView *>
. Both objects are inserted into the array without any complaints by the compilers or the runtime, but when you try to access the elements, you're warned by the compiler if the type of your variable is anything other than the generic type of the array (UIView
), even if it's one of UIView
's subclasses.
NSMutableArray<UIView *> *subviews = [[NSMutableArray alloc] init];
[subviews addObject:[[UIView alloc] init]]; // Works
[subviews addObject:[[UIImageView alloc] init]]; // Also works
UIView *sameView = subviews[0]; // Works
UIImageView *sameImageView = subviews[1]; // Incompatible pointer types initializing 'UIImageView *' with an expression of type 'UIView *'
NSLog(@"%@", NSStringFromClass([sameView class])); // UIView
NSLog(@"%@", NSStringFromClass([sameImageView class])); // UIImageView
Now, this produces a compile time warning, but does not crash at runtime. The key difference between this and the same example where the array's generic type is marked as __kindof
, is that the compiler won't complain if you try to access ones of its elements, and store the result in a variable who's type is UIView
or one of its subclasses.
NSMutableArray<__kindof UIView *> *subviews = [[NSMutableArray alloc] init];
[subviews addObject:[[UIView alloc] init]]; // Works
[subviews addObject:[[UIImageView alloc] init]]; // Also works
UIView *sameView = subviews[0]; // No problem
UIImageView *sameImageView = subviews[1]; // No complaints now!
NSLog(@"%@", NSStringFromClass([sameView class])); // UIView
NSLog(@"%@", NSStringFromClass([sameImageView class])); // UIImageView
__kindof
.