iOS开发—在@interface,@implementation和@property中变量的定义

来源:互联网 发布:上海游泳教练知乎 编辑:程序博客网 时间:2024/05/18 00:59

http://www.chirenhua.com/ios%E5%BC%80%E5%8F%91-%E5%9C%A8interfaceimplementation%E5%92%8Cproperty%E4%B8%AD%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89/?utm_source=tuicool&utm_medium=referral

一直搞不懂在OC中变量在@interface和@implementation中有什么区别,定义@property又有什么不同,查了很多资料,总结如下:

//ViewController.h@interface ViewController : UIViewController {    NSInteger a;}@property (nonatomic,assign) NSInteger b;@end//ViewController.m@interface ProductsViewController () {    NSInteger c;}@property (nonatomic,assign) NSInteger d;@end

a,b,c,d这四个变量有什么不同?

a是成员变量,相当于java中的protected,可以被子类继承。

b相当于java中的public,既可以被子类继承,也可以被外部访问。

c和d个人理解都是私有变量,相当于java中的private,不可以被子类调用,也不可以外部访问。

b 和 d 可以用 self.来访问, a和c不能。

a和c基本上很少用,一般都用b和d,如果需要对外开放的属性的就放在b,不需要的话直接在d位置定义。

a和c我觉的应该是为了兼容早期版本的写法,为了让你自己定义属性对应的内部变量,但是在arc之后的版本中,你只要定义一个属相,xcode会自动帮你定义一个以下划线打头的和属性同名的内部变量,所以你不需要另外再定义了.


0 0
原创粉丝点击