Private ivar in @interface or @implementation

前端 未结 3 1332
误落风尘
误落风尘 2020-12-05 18:32

Is there any reason to declare a private ivar in @interface instead of @implementation?

I see code like this all over the internet (includi

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 19:02

    Declaring instance variables in the @implementation is a recent feature of Obj-C, this is why you see a lot of code with them in the @interface - there was no other choice.

    If you are using a compiler which supports declaring instance variables in the implementation declaring them there is probably the best default - only put them in the interface if they need to be accessed by others.

    Edit: Additional Info

    Instance variables declared in the implementation are implicitly hidden (effectively private) and the visibility cannot be changed - @public, @protected and @private do not produce compiler errors (with the current Clang at least) but are ignored.

提交回复
热议问题