Xcode 7 new features lightweight generics lightweight generics with __kindof modifiers

Source: Internet
Author: User
Tags modifiers

Lightweight generics lightweight generics, lightweight because this is a pure compiler syntax support (LLVM 7.0), like nullability, without any OBJC runtime upgrades, that is, this new syntax can be used on Xcode 7 and fully backwards compatible (lower IOS version)

A container with a generic type

12
nsarray<nsstring *> *strings = @[@ "Sun" @ "Yuan"]; NSDictionary<nsstring *, nsnumber *> *mapping = @{@ "A": @1, @ "B": @2};

After the ID of the return value is replaced with a specific type, the moving code hints come out:

12
@property (readonly) Nsarray *imageurls; @property (readonly) nsarray<nsurl *> *imageurls;

Don't think about it and know what's in the array below, avoiding the chaos of NSString and Nsurl.

More fun than using a system generic container is to customize a generic class, and there's no documentation here, but it doesn't stop us from writing the test code, assuming we want to customize a Stack container class:

12345
@interfaceStack <ObjectType>: nsobject-(void) Pushobject: (ObjectType) object;-(ObjectType) popobject;@ Property (nonatomic, readonly) nsarray<objecttype> *allobjects; @end

This ObjectType is an incoming type of placeholder, it can only be defined on the @interface (class declaration, Class extension, category), if you like to be OK with T, this type in the scope of @interface and @end range is valid, you can use it as a The generic type of the parameter, the out parameter, and even the internal nsarray attribute, it should be said that everything is in line with expectations. We can also add type restrictions to ObjectType, such as:

1234
Only generic @interfacestack<objecttype:nsnumber *> that accept NSNumber *: nsobject//only accept generics nscopying that meet @interfacestack<o protocol Bjecttype:id<nscopying>>: NSObject

If nothing is added, the expression accepts any type (ID), and the compiler generates an error when the type is not satisfied.
Instantiate a Stack, everything works fine:

Comma-separated, others are the same, you can refer to the NSDictionary header file.

When a class supports generics, their type changes, such as the following three objects appear to be Stack, but actually belong to three types:

123
Stack*stack; Stack *stack<nsstring *>*stringstack; Stack<nsstring *>stack<nsmutablestring *>*mutablestringstack; Stack<nsmutablestring *>

When two of these types do type conversions, the compiler needs to know which conversions are allowed and which are forbidden, for example, by default:

__covariant-Covariance, subtypes can be strongly transferred to parent type (Richter substitution principle)
__contravariant-Inverse degeneration, the parent type can be strongly transferred to subtype (WTF?)

1
@interfaceStack <__covariantObjectType>: NSObject

Effect:

1
@interfaceStack <__contravariantObjectType>: NSObject

Effect:

__kindof

1
-(ID) Dequeuereusablecellwithidentifier: (NSString *) identifier;

Using a pointer to the UITableViewCell subtype is used to receive the return value, so the API is designed so that the developer does not have to write explicit strong turns each time the egg hurts, the return value is defined as the ID type, and the API actually means to return a UITableViewCell Or an instance of the UITableViewCell subclass, the new __kindof keyword solves the problem:

1
-(__kindof UITableViewCell *) Dequeuereusablecellwithidentifier: (NSString *) identifier;

The return value is clearly indicated and the user does not have to write a strong turn. To give an example with generics, the Subviews property of UIView is changed to:

1
@property (nonatomic, readonly, copy) nsarray<__kindof UIView *> *subviews;

This way, there is no warning when writing the following code:

1
UIButton *button = View.subviews.lastObject;

Where to go

At the same time, the personal feel of the new version of Xcode on the inheritance chain constructor detection also strengthened, Ns_designated_initializer this macro is not a new face, you can use it to mark the same as Swift , the constructor and the convenient constructor.

Finally, with a piece of code that uses all the new features,Swift is a trend, and if you are still writing objective-c code for the time being, use all the new features, perhaps making your migration to the new language painless.

Click to open link

The above describes the new features of Xcode 7 lightweight generics lightweight generics and __kindof modifiers, including aspects of the content, I hope to be interested in the development of iOS friends are helpful.

This article site link: http://www.codes51.com/article/detail_176428.html

Xcode 7 new features lightweight generics lightweight generics with __kindof modifiers

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.