Objective C - Using property get accessor vs directly using iVar

前端 未结 3 703
陌清茗
陌清茗 2021-01-21 10:56

I was wondering what exactly are the differences between using the (get) accessor for reading the value of property and directly using the iVar?

Say I have a class which

3条回答
  •  囚心锁ツ
    2021-01-21 11:40

    I am not a very experienced person to answer this question, even though I am trying to give my views and my experience by seeing source code which is around 10yrs older.

    In earlier codes they were creating ivars and property/synthesise. Nowadays only property/synthesise is used.One benefit I see is of less code and no confusion.

    Confusion!!! Yes, if ivars and its property are of different name, it does create a confusion to other person or even to you if you are reading your own code after a while. So use one name for ivar and property.

    By using property KVO/KVB/KVC are handled automatically, thats for sure.

    @property/@synthesise sets your ivar to 0/nil etc.

    Also helpful if your subclass contains same ivar.

    For mutable objects Dont make properties.

提交回复
热议问题