Key words
- IOS9 New keyword: Used to modify the property, or the parameters of the method, the return value of the method
-
Benefits:
- Cater to Swift
- Improve our developer development specifications and reduce communication between programmers
-
Note: iOS9 The new keyword nonnull,nullable,null_resettable,_null_unspecified only修饰对象,不能修饰基本数据类型
/** 1. 首字母不带下滑线的修饰类名(eg. nullable) 2. 首字母带一个下划线,则首字母大写,修饰对象(eg. _Nullable) 3. 首字母带两个下划线,则首字母小写,修饰对象(eg. __nullable) 4. 大多数一般只有第一种*/
nullable
Function: Indicates that can be empty
// 方式一:@property (nonatomicstrongNSString// 方式二:@property (nonatomicstrongNSString// 方式三:@property (nonatomicstrongNSString *__nullable name;
nonnull
Function: cannot be empty
nonnullnon:非 null:空书写格式: @property (nonatomic, strong, nonnull) NSString *icon; @property (nonatomic, strong) NSString * _Nonnull icon; @property (nonatomic, strong) NSString * __nonnull icon;
In NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END between, all object properties and methods defined by default are nonnull
null_resettable
Function: Get: Cannot return NULL, set can be empty
// 书写方式:@property (nonatomicstrongNSString *name;// 注意;如果使用null_resettable,必须 重写get方法或者set方法,处理传递的值为空的情况
_Null_unspecified
Function: Not sure whether it is empty
// 书写方式只有这种 // 方式一 @property (nonatomicstrongNSString *_Null_unspecified name; // 方式二 @property (nonatomicstrongNSString *__null_unspecified name;
Generic (restriction type) usage scenarios
- restricting types in collections (arrays, dictionaries, nsset)
Note: The invocation of a method can only be detected because the declared generics can only be placed in the method
- When a class is declared, the properties of an object are indeterminate, and only when the object is created, you can use the generic
Benefits
- Improve development specifications and reduce communication between programmers
- The object is taken out of the collection, directly as a generic object and can be used directly using point syntax
Writing format
Placed behind the type, indicating the restriction of this type.
@property (nonatomicstrongNSMutableArray<NSString *> *datas;
Custom generics: Emulating arrays
- Requirement: Suppose there is a person who can program the language, but is not sure at the time of definition, only when the object is created.
- The type of the language attribute is a particular
- ID Type: Indicates that any object can be passed
- Launguage type, no prompt when assigning value
- Generics, declaring generics, when you create an object, you determine the generic, and you are prompted for the assignment.
@interface Person<ObjectType> : NSObject {// 语言@property (nonatomic) ObjectType language;}
Covariance, contravariance
Function: for conversion type
- Default with generic variables, mutual assignment has reported warning, using covariance, inversion, can be solved.
- Covariant (__covariant): transition upward, subclass to parent class
- Invert (__contravariant): Shift Down parent class rotor class
__kindofid Disadvantages
- Cannot check the actual type at compile time
- return value, no prompt
- Can call any method, compile without error
Instancetype
- The class of the current object is automatically recognized, returning the object of the current calling class
+ (instancetype)person;
__kindof:
When called, it is clear that the return type (similar to Instancetype, but instancetype can only be used in initialization)
Role:
1. Specify this type as a subclass of Class_name or class_name, telling the compiler that both can be adapted.
2. Objective-c is a dynamic type, the compiler will do type matching at compile time, there will be no compile warnings, and no error.
Scene:
1. Generics
2. Method return value
//- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; orindexisoutof range;// returns nil if cell is not @propertyreadonly) NSArray<__kindof UITableViewCell *> *visibleCells;
IOS9 new features slowly finishing