0

I just wanted to know if it is necessary to declare a property for every private variable I create in my Xcode projects. If it is, can someone explain me the reason? Thanks =)

1 Answers1

1

Properties are useful if you want to add custom get-and-set logic for your variables. They typically are used more for public variables. There are certainly cases when you may want to also use them for private variables, but it is not "necessary".

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • You should add that the alternative is to use an instance variable. Used to be that went without saying, since you had to declare the instance var for each property (so just omitting the @property statement gave you the instance var), but now declaring a property implicitly defines the instance var. – Hot Licks Sep 03 '13 at 18:48
  • Can instance variables be used with KVC? – Pétur Ingi Egilsson Sep 03 '13 at 18:51
  • 2
    @Pétur Yes, but 1. that breaks encapsulation and 2. it's another question. – Nikolai Ruhe Sep 03 '13 at 19:08