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

后端 未结 4 943
难免孤独
难免孤独 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: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.

提交回复
热议问题