OC-@property和@synthesize关键字

来源:互联网 发布:linux proc net dev 编辑:程序博客网 时间:2024/06/10 05:57
1、@property关键字介绍

       @property是编译器的指令
       什么是编译器的指令,编译器指令是用来告诉编译器要做什么
        @property告诉编译器声明属性的访问器(getter/setter)方法
  好处:免去我们手工写get和set方法。
2、用法:@property   类型名   方法名(去掉set)
        如 @property  int  age;
        相当于进行了进行了age的 set 和 get 方法的声明
       -(void)setAge:(int)age;
       -(void)age;
3、作用:
    1、在xcode4.4 之前,用于帮我们实现get/set方法的声明
    2、在xcode4.4 之后,有增强功能

4、注意事项
     在老式的代码中
      @property只能写在@interface  @end 中
      @property用来自动 生成成员变量的 get/set方法声明(xcode.4.4以前)
      告诉property要生成的 get/set 方法声明的成员变量类型是什么
      告诉property要生成的 get/set方法是哪个属性的,属性名称去掉下划线


1、@synthesize介绍
     @synthesize是在 .m  文件中定义 set和get 方法的实现
2、用法:
        @property   int  age;  写在   .h 文件 @interface  @end中
        @synthesize   age ;    写在 .m 文件 ,@implementation  @end 中  表示实现  age 的 get 方法
         注意  要想在 . m 文件这样写[url=][url=][/url][/url][url=][url=]@synthesize[/url][/url] age;  
        必须  先在 .h 文件中使用 @ property  int age;
     @property和@synthesize 搭配使用,用于简化 set 和 get 方法的定义和实现
3、指定操作实例变量的@synthesize
     [url=]@synthesize[/url] 方法名 = 实例变量名
     当指定实例变量名以后,此时再不会操作默认的实例变量了
     @synthesize age = _age,weight=_weight;
     @synthesize name = _name;

本帖主要是写   xcode 4.4 以前@property 和@synthesize关键字
xcode 4.4以后 请看   @property增强使用



0 0