Why does this ivar need @protected if @protected is the default?

扶醉桌前 提交于 2019-12-07 10:45:20

问题


@interface AClass : SomeType {
@protected
    NSMutableArray* amINotAlreadyProtected; //?
}

Why does this code need @protected if @protected is the default? This code was written by a very experienced programmer, but I would omit the specifier myself.


回答1:


There is no need for the keyword @protected as it is the default behavior.

However, some programmers tend to use it anyways incase a less experienced programmer comes along at a later date and doesn't know this. It can also be mentioned that it increase code readability incase there are some variables that are protected and other private or public.




回答2:


It is from an age when you might see:

@interface Foo:Bar
{
     @private
     … ivars …
     @protected
     … ivars …
}
…
@end

That is, while @protected is the default, you would need to use it if you had switched to one of the other variants and wanted to switch back. And, yes, there were reasons (often bad ones) to ensure that ivar declaration order was preserved from release to release.

Beyond that, including a keyword for the default case ensures that pedantic grey beards (like myself) can be exactly explicit in their declarations.

However, modern additions like @property mean that such shenanigans are no longer necessary.



来源:https://stackoverflow.com/questions/17176702/why-does-this-ivar-need-protected-if-protected-is-the-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!