Why does this ivar need @protected if @protected i

2019-05-05 01:36发布

@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.

2条回答
爷的心禁止访问
2楼-- · 2019-05-05 02:36

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.

查看更多
聊天终结者
3楼-- · 2019-05-05 02:38

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.

查看更多
登录 后发表回答