Objective-c with the upgrade of Xcode 7 brings a lot of new features, of course, the most important update is the introduction of Objective-c's lightweight generics, specifically the Objective-c class of lightweight generics. In addition, there is a small feature is the introduction of the __KINDOF keyword.
Some technology sites claim that __KINDOF is generally used for generics, such as: Nsarray<__kindof uiview*> means that UIView objects or UIView subclass objects are stored in this Nsarray object. However, since objective-c is a dynamic type, and the compiler will do type matching at compile time, even if we use nsarray<uiview*> to store Uiscrollview objects in this array, there will be no compile warning and no error. So what's the use of this __kindof?
__kindof class_name is intended to designate this type as a subclass of class_name or class_name , telling the compiler that both can be adapted. There is now a case for compiler type check matching, which is used in conjunction with generics in the C11 standard! Since type matching in the generic selection expression in C11 is completely matched by the compiler at compile time, there is no runtime action (unless it is a dynamically variable-length type, such as the int[n] type), so this can be done by the __kindof Class_name form to do some of the wider range of type matching. such as the following code:
@interfaceMyobject:nsobject@end@implementationMyObject@end@implementationViewcontroller- (void) viewdidload {MyObject*obj =[[MyObject alloc] init]; intx = _generic (obj,int:0, nsstring*:1, nsobject*:2,default:-1); NSLog (@"x1 =%d", x); X= _generic (obj,int:0,float:1, __kindof nsobject*:2,default:-1); NSLog (@"x2 =%d", x); [obj release];}@end
Because the compiler will check at compile time, so the second _generic here can not use nsstring*, otherwise the compiler will think nsstring* and __kindof nsobject* are mutually compatible types, resulting in matching ambiguity.
About the objective-c new __kindof keyword