/*
新出的關鍵字:修飾屬性,方法的參數(shù),方法返回值,規(guī)范開發(fā)。
好處:
1.提高程序員規(guī)范,減少交流成本,程序員一看,就知道怎么賦值。
注意:只能用于聲明對象,不能聲明基本數(shù)據(jù)類型,因為只有對象才能為nil。
nonnull:表示屬性不能為空,non:非,null:空
方式一:
@property (nonatomic, strong, nonnull) NSString *name;
方式二:
@property (nonatomic, strong) NSString * _Nonnull name;
方式三:
@property (nonatomic, strong) NSString * __nonnull name;
在NS_ASSUME_NONNULL_BEGIN與NS_ASSUME_NONNULL_END之間所有的對象屬性,方法參數(shù),方法返回值,默認都是nonnull。
NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong) NSString *name;
NS_ASSUME_NONNULL_END
*/
/*
nullable:可以為nil
方式一:
@property (nonatomic, strong, nullable) NSString *name;
方式二:
@property (nonatomic, strong) NSString * _Nullable name;
方式三:
@property (nonatomic, strong) NSString * __nullable name;
*/
/*
null_resettable:可以重新設置空,set方法可以為空,get不能為空。
方式一:
@property (nonatomic, strong, null_resettable) NSString *name;
注意:用null_resettable屬性,必須重寫set,或者get方法,處理傳值為nil的情況,可以模仿控制器view的get方法,當view為nil,就自己創(chuàng)建一個.
*/
/*
_Null_unspecified:不確定是否為空.
方式一:
@property (nonatomic, strong) NSString * _Null_unspecified name;
*/
@property (nonatomic, strong ,nonnull) NSString *name;
@property (nonatomic, strong ,nullable) NSString *icon;
@property (nonatomic, strong ,null_resettable) NSString *text;
@end
@implementation ViewController
- (NSString *)text
{
if (_text == nil) {
_text = @"我不能為空";
}
return _text;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#import "ViewController.h"
#import "Person.h"
#import "IOS.h"
#import "Java.h"
@interface ViewController ()
// 定義泛型:確定類型
@property (nonatomic, strong) NSMutableArray<NSString *> *array;
@end
@implementation ViewController
// 泛型:限制類型
// 開發(fā)中使用場景:
// * 限制集合中的類型,注意:只能檢測方法的調用,因為聲明的泛型,只能放在方法中
// * 當一個類在聲明的時候,某個對象的屬性不確定,只有創(chuàng)建對象的時候才確定,可以使用泛型
// 泛型書寫格式:放在類型后面,表示限制這個類型.
// 好處:
// 提高程序員開發(fā)規(guī)范,減少交流成本。
// 從數(shù)組或者字典取值,都是id類型,不能調用點語法,但是使用泛型,就可以了。
// 自定義泛型:模仿數(shù)組
// 需求:假設有個Person,這個人會編程語言,但是在定義的時候不確定,只有在創(chuàng)建對象的時候才確定。
// language屬性的類型就有講究了
// id 類型:表示可以傳任何對象
// Launguage類型,在賦值的時候沒有提示
// 泛型,聲明泛型,在創(chuàng)建對象的時候,確定泛型,在賦值就有提示了。
// 泛型中協(xié)變,逆變,用于轉換類型
// 默認帶有泛型的變量,互相賦值有報警告,使用協(xié)變,逆變,就能解決.
// 協(xié)變(__covariant): 向上轉型, 子類轉父類
// 逆變(__contravariant):向下轉型 父類轉子類
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Person<Language *> *person = [[Person alloc] init];
// person.language = [[IOS alloc] init];
Person<IOS *> *person1 = [[Person alloc] init];
person = person1;
person1 = person;
// person1.language = [[Language alloc] init];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
}
// __kindof:相當于,表示某個類或者他的子類。
// 設計模型中可以使用,當給某個類提供類方法,想讓外界調用能看到創(chuàng)建什么對象,并且不報警告。
-
(void)viewDidLoad {
[super viewDidLoad];SonPerson *person = [SonPerson person] ;
}