objective-cpropertiesreactive-cocoaivar

In objective-c what if a property's backup ivar is declared explicitly?


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.


Solution

  • I just wondered if the signal's backup ivar is the _signal

    Maybe...

    That leaves two, primarily historical, options:

    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