一。早期只能定義在.h文件中。用@private 關鍵字來定義私有變量。
@interface ViewController{
@private Bool _isBool;
}
@end
二。允許在.m文件中添加一個匿名的類別Category 來添加屬性。
@interface ViewController{
@property(nonatomic,assign)Bool isBool;
}
@end
三。允許在.m文件中 implementation中直接添加私有變量。
@implmentation ViewController{
Bool _isBool;
}
@end
總結:屬性@property,一共做了三件事:
1.創建實例變量。
2.生成setter方法。
3,生成getter方法。
屬性會成成一些,無用的方法,造成IPA過大。使用成員變量的方式,運行的速度更快。
當開發需要懶加載的時候(需要的時候再加進去),一般使用@property。
-(Bool)isBool{
if(!_isBool)
{
return NO;
}
return isBool;
}
建議:不需要使用懶加載的時候,直接創建_isBool,實力變量。