Objective:
In iOS 9 Apple introduced a lot of keywords, the purpose is actually very clear, mainly to improve the efficiency of developers, to benefit the communication between programmers, in the development of code more standardized!
1. Nullable and Nonnull
Nullable: Indicates that it can be nil
Nonnull: Indicates that nil is not allowed
These two keywords can only decorate the object, cannot decorate the basic data type, can use in the property, the method parameter, the method return value uses, by default, does not add nullable, the setter and the getter all can be nil
Let's take a look at how to use nullable as an example:
@property (nonatomic, strong, nullable) nsarray * Array;
@property (nonatomic, strong) nsarray * __nullable Array ;
@property (nonatomic, strong) nsarray * _nullable Array ;
How to use it in the method? Let's take a look at:
- (nonnull nsstring *)teststring:(nonnull nsstring *) string;
- (nsstring * _nonnull)teststring:(nsstring * _nonnull) string;
- (nsstring * __nonnull)teststring:(nsstring * __nonnull )string;
When we call the property or the method, the system will prompt you whether the current object is nil such as:
There are 2 additional macros as follows:
Ns_assume_nonnull_begin
Ns_assume_nonnull_end
All object properties between the two macros are nonnull by default
Keyword: null_resettable
Getter: Can not be nil
Setter: can be nil
If you use Null_resettable, you must override the getter or setter method. The purpose is to handle cases where the value is empty
Here's how to use it:
@property (nonatomic, strong, null_resettable) UIView *testview;
Keyword: _null_unspecified
Not sure if it is empty, this keyword personally feels not very significant, because the object property defaults to be indeterminate
Here's how to use it:
@property (nonatomic, strong) UIView * _null_unspecified TestView;
@property (nonatomic, strong) UIView * __null_ Unspecified TestView;
2. Generics
By using generics, we can easily get the elements in them and access their unique properties and methods, which are generally used in collections (for example: arrays, dictionaries), and when method calls are effective, let's look at how to use:
Property uses
@property (nonatomic, strong) nsmutablearray *testarraym;
Using in methods
- (nsarray *)Teststringarray:(nsarray *) Stringarray;
We use generics in arrays, and then we can clearly know what types are in the array, such as:
Use generic. png in methods
Using generics. png in Nsmutablearray
And, as we can see, the type of the object in the set, the object by the collection can be used as generics, and can be directly with the point syntax, it is conceivable that the benefits of generics are very powerful!
Covariant/contravariant
3. __kindof
Represents the current class, or its subclasses (__kindof use: Put in front of the type, indicating adornment of this type)
Let's start with the ID first.
At first the return value of our method is indeterminate, we will use ID but it will be bad to use it, ID cannot check the real type at compile time, and the return value is not prompt
- (ID)teststringarray:(nsarray *)stringarray;
The return value uses the ID without prompting. png
So Apple appeared instancetype it automatically recognizes which class the current object is, and can only be used in the return value, but still doesn't know what type to return when calling the method
- (instancetype)teststringarray:(nsarray *)stringarray;
Instancetype can automatically identify types in compilation. png
Finally Apple launched __kindof it was clear at the time of the call to know the type of return, before we need to force type conversion in many places, it is convenient to have a lot of
- (__kindof nsarray *)Teststringarray:(nsarray *) Stringarray;
__kindof using. png
Easy use of IOS 9 keywords