Say that I have the following public methods on my class:
- (BOOL)finished
{
return _finished;
}
- (void)setFinished:(BOOL)aValue
{
_finished = aValue;
}
Do I also need to set finished as a property on the class:
@property SomeClass *finished;
I may have misunderstood @property
, but to me it seems like I am repeating myself.
@property
or just one of them?By declaring a @property
, the LLVM compiler will automatically "synthesize" your accessor methods (getter and setter), as well as an instance variable (ivar
), denoted by an underscore prefix (_finished
, for example)
In your case there, because you aren't doing anything special with your accessors (you are just directly reading/writing to the backing ivar
), simply declaring a @property
will do the trick. You won't need to write the -finished
and -setFinished
methods
If you do not declare @property
, you'll have to declare your ivar
@property
also indicates you are working with a member variable of an object, and as user @Sulthan says in the comment, each class holds a list of @properties
that can be searched
And another good point by the same user, @property
makes your accessor methods atomic
, which protects your ivar
from being read while it's being written
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With