1.屬性nonnull \__nonnull 修飾時:該屬性的setter和 getter都不能爲nil
例如:
@property (nonatomic,strong,nonnull)NSArray *names;
@property (nonatomic,strong) NSArray *__nonnull names;
默認情況下,不加nullable, setter和 getter 都是可以爲nil
nullable更多的作用在於程序員之間的溝通交流(提醒同事某個屬性可能是nil)
例如:
@property (nonatomic,strong,nullable) NSArray *names;
@property (nonatomic,strong)NSArray *__nullable names;
3.屬性null_resettable修飾時 : setter可以爲nil, getter不可以爲nil
@property (null_resettable, nonatomic, strong) NSArray *names;
4.如果
設置@interfaceViewController ()
@end 之間的所有屬性默認都是nonnull,可以添加 NS_ASSUME_NONNULL_BEGIN 與NS_ASSUME_NONNULL_END描述。
例如:
NS_ASSUME_NONNULL_BEGIN
@interface ViewController ()
@end
NS_ASSUME_NONNULL_END
5.方法返回值中添加__kindof修飾,這樣可以解決當返回值用子類接收時,不用強制轉換處理。
例如:
// __kindof : 告訴編譯器返回值可能是NSString,也可能是NSMutableString
+ (__kindof NSString *)dequeueXXX;
NSString *str = [self dequeueXXX];
[str length];
NSMutableString *str2 = [self dequeueXXX];
[str2 length];