iOS development iOS9 new feature keyword learning: generics, contravariance, covariance, __kindof

Source: Internet
Author: User

A: How to learn? What are you going to learn?

1: Learn the design ideas of excellent projects, ask a few why, why so design, so what are the benefits of design, but also can be optimized, how to apply to their own projects 2: Learn the code style of excellent projects, the code of the packaging design ideas, why so design, so the benefits of design is what, can also be optimized, how to apply to their own projects, each line of code to write attentively, each line of code to strive to make the most concise

3: Learn how to analyze problems and how to solve them

4: How to learn new things how to study: 1: First study to learn what role is what, what are the benefits 2: How to use: specific grammar knowledge, referring to Apple's API 3: Usage scenario: What is generally used in the project.

5: The most important thing is to spend a lot of time to study excellent code, than others think deeper than others think, pay attention to every detail, every detail to understand, every detail to achieve the ultimate, spend a lot of time to practice, will learn the knowledge applied to the project.

II: Nullable, nonnull,null_resettable,_null_unspecified of the key words of understanding

/*1: How to study new features? 1. Use the new Xcode to create the project, use the old Xcode to open Xcode7 iOS9 Xcode6 iOS8 Xcode5 iOS7 Xcode4 1. What are the new features?    IOS9: Keywords: can be used for attributes, method return values and parameters in the role of the keyword: hint action, tell developer Property Information keyword Purpose: To cater to Swift,swift is a strong language, Swift must specify whether an object is NULL keyword benefits: Improve code planning, reduce communication costs The keyword simply provides a warning and does not report a compilation error*//*2:nullable:nullable:1. How to use (syntax) 2. When to use (function) nullable effect: can be null nullable Syntax 1 @property (nonatomic, Strong,     Nullable) NSString *name;     Nullable Syntax 2 * keyword variable name @property (nonatomic, Strong) NSString * _nullable name;  Nullable Syntax 3 @property (nonatomic, Strong) NSString * __nullable name; *//*3:nonnull nonnull:1. How to use (syntax) 2. When to use (function) nonnull function: cannot be null nonnull Syntax 1 @property (nonatomic, Strong, nullable) Nsstrin  G *name;  nonnull Syntax 2 * keyword variable name @property (nonatomic, Strong) NSString * _nonnull name;  nonnull Syntax 3 @property (nonatomic, Strong) NSString * __nonnull name; *//*4:null_resettable:null_resettable:1. How to use (syntax) 2. When to use (function) Null_resettable: must be handled as nil, overriding the Get method Null_   resettable function: The Get method cannot return Nil,set can be passed in as null null_resettable Syntax 1 @property (nonatomic, Strong, null_resettable) NSString *name;  -(NSString *) name {if (_name = = nil) {_name = @ "";} return _name;} *//*5:_null_unspecified: Not sure whether it is empty*//*6:1: keyword note between ns_assume_nonnull_begin and ns_assume_nonnull_end the default is Nonnull, the former is written under the header file, the latter is written above the end 2: The keyword cannot be used for basic Data type (int,float), nil is used only for object 3: @property (nonatomic) NSString *name, so the default is strong decorated*/#import "ViewController.h"@interfaceViewcontroller ()//nonnull//no processing for empty conditions@property (Nonatomic, Strong, nonnull) NSString *name;@end@implementationViewcontroller//-(UIView *) View//{//if (_view = = nil) {//[self loadview];//[self viewdidload];//    }//return _view//}- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.    }@end

Three: generic type

#import "ViewController.h"/*   1: Generics Introduction generics: Why do restricted types launch generics? cater to Swift generics: 1. Limit type 2. Improve code planning, reduce communication costs, and see what's in the collection. Generic definition Usage: type < restriction type >:nsmutable Array<nsstring *> *arr, the array contains the string type 2: generic declaration of the class: generic declaration: When declaring a class, after the class < generic name;: @interface Person<objecttype            >: NSObject generics are just warning generic benefits: 1. From the array, you can use the DOT syntax: the type of the element in the array is the ID type, the ID is not the point syntax, but the generic type is used to restrict the type, then the point syntax can be exploited after the object is fetched from the array. 2. Adding elements to an array, Tip 3: Generics are used in development scenarios: 1. Used to restrict collection types: (The collection class includes Nsarray and Nsset, both using the same, the former is ordered, the latter is unordered) why the collection can use generics? Using generics, you must Declare generics first?    = = How to declare a generic custom generic type?         When do I use generics? 2: When declaring a class, not certain properties or method types, when using this class is determined, you can take the generic custom person, some programming language (Ios,java), in declaring the person, not sure what the man will be, Use person to know what language this person will be if the generic is not defined. The default is ID for example: 1; Declare generic #import <Foundation/Foundation.h> @interface Per         Son<objecttype>: NSObject//Language @property (nonatomic, strong) ObjectType language; @end 2: using: Java *java = [[Java alloc] init];  iOS *ios = [[iOS alloc] init]; IOS person<ios *> *p = [[Person alloc] iniT];  P.language = iOS; Java Person<java *> *p1 = [[Person alloc] init];  P1.language = java;*/#import "Person.h"#import "Java.h"#import "iOS.h"@interfaceViewcontroller () @property (nonatomic, strong) Nsmutablearray<nsstring *> *arr;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Java*java =[[Java alloc] init]; IOS*ios =[[IOS alloc] init]; //IOSPerson<ios *> *p =[[Person alloc] init]; P.language=iOS; //JavaPerson<java *> *P1 =[[Person alloc] init]; P1.language=Java;}- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{    }@end

Four: Covariance and contravariance of generics

#import "ViewController.h"/*  1: Generics: Why does the restriction type introduce generics? cater to Swift generics: 1. Limit type 2. Improve code planning, reduce communication costs, and see what's in the collection. Generic definition Usage: type < restriction type > generic declaration: When declaring a class Wait, at the back of the class < generic name > generics is only a warning generic benefit: 1. From the array, you can use Point Syntax 2. Adding elements to an array, prompting the generic to use the scenario in development: 1. To restrict the collection type ID is not to make With dot syntax Why can a collection use generics? Using generics, you must declare generics first.    = = How to declare a generic custom generic type? When to use generics? When declaring a class, unsure of certain properties or method types, when using this class, you can take a generic custom person, some programming language (Ios,java), declaring the person, not sure what this man will be, Use person to know what language this person will be if the generic is not defined. By default, ID is used for parent-child type conversion generics: __covariant: Covariant, subclass to parent class: that is, assigning a pointer to a subclass to a subclass __contravar Iant: The inverse of the parent class: That is, the pointer to the parent class is assigned to the subclass generic note point: In the array, generally with a variable array to add methods, generics will take effect, if the use of non-variable groups, add elements, generics have no effect, just hint of the role of 2: Inheritance: Subclass inherits the parent class, The parent class can expose methods in. h For example, the initialization method for subclasses to inherit 1: The parent class only exposes the initialization method is not overridden, after the subclass inherits, the subclass can rewrite, (subclass when overriding, try to use self, do not use the class name, to avoid other classes in inheriting the class, the initialization is the object of the class, Instead of inheriting the object of the child class. After the subclass is overridden, the self in the parent class is the object of the subclass, where many subclasses inherit the same parent class, and the parent class can provide a get identity method for subclasses to override the return identity, thus differentiating between subclasses 2: The parent class provides methods for subclasses to inherit, and the parent class implements the method, When the subclass calls the initialization method externally, it invokes the initialization method implemented in the parent class.   (for example, the Sina parametric model, which inherits from the parent class), if the subclass overrides the initialization method of the parent class, calling Super executes the parent class's method, otherwise it will not execute the parent class's method, overwriting the parent class's method, leaving only the subclass's method*/#import "Person.h"#import "Java.h"#import "iOS.h"@interfaceViewcontroller () @property (nonatomic, strong) Nsmutablearray<nsstring *> *arr;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; [_arr AddObject:@"123"];//_arr = @[@ "213", @ "213", @1]; //additional setup after loading the view, typically from a nib.IOS*ios =[[IOS alloc] init]; Language*language =[[Language alloc] init]; //Parent class Rotor classPerson<language *> *p =[[Person alloc] init]; P.language=language; //IOSPerson<ios *> *P1 =[[Person alloc] init]; P1=p; }//sub-class to parent class- (void) covariant{IOS*ios =[[IOS alloc] init]; Language*language =[[Language alloc] init]; //IOSPerson<ios *> *p =[[Person alloc] init]; P.language=iOS; //LanguagePerson<language *> *P1; P1=p;}- (void) test{Java*java =[[Java alloc] init]; IOS*ios =[[IOS alloc] init]; //IOSPerson<ios *> *p =[[Person alloc] init]; P.language=iOS; //JavaPerson<java *> *P1 =[[Person alloc] init]; P1.language=Java;}@end

V: __kindof

#import "ViewController.h"#import "SubPerson.h"/*kindof: Equivalent to __kindof: Represents the current class or its subclass ', Class design history ID: Can call any object method, can be used as a parameter or return value, cannot be compiled check Instancetype: Automatically recognize the object of the current class, Objects that are the same as the current class type are returned automatically only as a return value that cannot be used as a parameter*/@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; /** * When the parent class provides the method of initialization, the subclass inherits the method of the parent class, and if the subclass Subperson invokes the initialization method of the parent class: person, the type of the parent class object is returned, and a warning appears, in which case the initialization method can be defined in the parent class with __ Kindof to modify to indicate the current class or its subclasses.     + (__kindof person *) person; */Subperson*p =[Subperson person]; }@end

iOS development iOS9 new feature keyword learning: generics, contravariance, covariance, __kindof

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.