In ReactiveCococa 2.5, RACMulticastConnection has a signal property, which is a RACSignal, while in its implementation, it has a ivar _signal, which is a RACSubject. I just wondered if the signal's backup ivar is the _signal, how to explain it?
ok, it seems that my question isn't clear, I put some demo code here, hope someone can answer it.
the header file:
@interface RACMulticastConnection : NSObject
@property (nonatomic, strong, readonly) RACSignal *signal;
...
@end
the impl file:
@interface RACMulticastConnection () {
RACSubject *_signal;
...
}
...
One more thing, there is no @synthesize code in its implementation and as you all known, RACSubject is a subclass of RACSignal.
I just wondered if the signal's backup ivar is the _signal
Maybe...
@synthesize signal
of any form then the property is auto-implementated (the standard approach) and the ivar _signal
will be used as the backing variable. If there was no ivar _signal
then one would be automatically created.That leaves two, primarily historical, options:
If there is an @synthesize signal
then that will introduce an ivar signal
to use as the backing variable, and the declared ivar _signal
will not be associated in anyway to the property.
Finally if there is an @synthesize signal = _signal
then the ivar _signal
will be used as the backing variable.
Addendum: After comment/question edit
Your edits to the question simply fix the above answer to the first bullet (the no @synthesize
case).
Maybe you are concerned over the property having the type RACSignal *
while the ivar has the type RACSubject *
?
Remember the property is readonly
so the only way to assign to it is using an assignment to _signal
within the implementation, and the compiler will check that such an assignment has the type RACSubject *
(or a subtype).
A client of the class reads the value using the property and is told the value is of type RACSignal *
, and it is as RACSubject
is a subclass of RACSignal
so everything is type correct.
Using RACSubject *
for the ivar is a way to improve checking within the implementation - i.e. the property returns an RACSignal *
but the implementation knows that it will always return the subclass RACSubject
so why not state that so the compiler will catch cases when it does not?
To demonstrate the compiler is checking try changing the type of _signal
, say to NSArray
, and you will get a compiler error stating that the type does not conform to the one required by the property.
HTH