About the objective-c new __kindof keyword

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.