What's the difference between adding pseudo-private ivars in a class extension or in the @implementation block?

后端 未结 2 1753
一个人的身影
一个人的身影 2020-12-10 18:55

What\'s the difference between putting pseudo-private instance variables in a class extension inside the .m file, or putting them in the newly introduced @implementation bra

相关标签:
2条回答
  • 2020-12-10 19:28

    The main difference between the two approaches is that you can include the class extension in a separate header, whereas the @implementation ivars obviously have to go with the @implementation block in the .m file (and there can only be one @implementation for a given class (extensions not included)). The practical result of this is that you can have multiple levels of "private" ivars:

    • MyClass.h: public ivars
    • MyClass+Private.h: semi-private ivars
    • MyClass.m: really private ivars

    As a hypothetical example, pretend that MyClass is UIView. In that case, UIView.h is the header that we can all access, UIView+Private.h is the "private" header than only Apple can access, and UIView.m has stuff that only the people specifically responsible for UIView need to know about.

    0 讨论(0)
  • 2020-12-10 19:33

    Personally, I prefer to put my ivars in a single class extension in the implementation file, I think it's cleaner that way. I don't think there are any performance advantages or consequences to using one or the other, it's more about being able to code the way you want to.

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