IOS 私有变量 私有属性的书写方法

一。早期只能定义在.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,实力变量。

上一篇:在View页面,使用@if(){ }输出判断正确的内容


下一篇:Axure RP7.0 使用记录手册