【问题标题】:What are @property and @synthesize used for in Objective-C?在 Objective-C 中,@property 和 @synthesize 是做什么用的?
【发布时间】:2010-09-06 05:31:59
【问题描述】:

@property@synthesize 有什么用?可以举个例子解释一下吗?

【问题讨论】:

标签: objective-c


【解决方案1】:

非常简短的回答:他们为 ivars 创建访问器。

some examples on wikipedia。看看那些。

【讨论】:

    【解决方案2】:

    来自apple developer library

    您可以将属性声明视为等同于声明两个访问器方法。因此

    @property float value;
    

    相当于:

    - (float)value;
    
    - (void)setValue:(float)newValue;
    

    通过使用@synthesize,编译器会为您创建访问器方法(查看更多here

    【讨论】:

      【解决方案3】:
      // Sample for @property and @sythesize //
      
      @interface ClassA
      
        NSString *str; 
      
      @end
      
      @implementation ClassA
      
      @end
      
      The Main Function main()
      

      //确保你#import ClassA

      ClassA *obj=[[ClassA alloc]init];
      
      obj.str=@"XYZ"; // That time it will give the error that we don't have the getter or setter method. To use string like this we use @property and @sythesize
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        • 2013-11-19
        • 2011-11-21
        • 2011-07-05
        • 2012-03-11
        相关资源
        最近更新 更多