Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 479
Dave Chambers Avatar asked Mar 23 '23 12:03

Dave Chambers


2 Answers

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.

like image 62
Groot Avatar answered Apr 19 '23 08:04

Groot


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.

like image 42
bbum Avatar answered Apr 19 '23 09:04

bbum