What's the difference between @property and @synthesize?

后端 未结 4 937
难免孤独
难免孤独 2020-12-16 07:55

Like I understand, @synthesize actually is generating the Getters and Setters. But what\'s @property then doing? Is it just setting up the parameters for that cool @synthesi

相关标签:
4条回答
  • 2020-12-16 08:25

    @property declares the name as a property. This means, it will be accessible via the dot syntax (object.value).

    @synthetize can be seen as a macro, that creates the getter and setter methods. It is useful to know that you can override those methods, even if you type have the @synthetize in place.

    0 讨论(0)
  • 2020-12-16 08:33

    @property declares getter and/or setter

    @synthesize implements them.

    0 讨论(0)
  • 2020-12-16 08:33

    There is a common misconception that the @synthesize directive is required in order to implement setters and getters created with the @property directive, but this is not the case. Using the @property directive without @synthesize will still generate setters/getters and allow you to use dot notation. However, leaving out the @synthesize directive will cause the compiler to generate corresponding instance variables with a leading underscore character, e.g. the property myVar will have an instance variable of _myVar.

    Using a leading underscore is a common convention that allows you to differentiate between properties and instance variables. It is also common for properties and instance variables to share the same name, which is what @synthesize does by default.

    0 讨论(0)
  • 2020-12-16 08:34

    You write @property in header file

    @property float value;
    

    is equivalent to:

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

    It get information for OTHER classes, that your class has this methods

    @synthesize phisicaly CREATE these methods in class implementation

    0 讨论(0)
提交回复
热议问题