ReactiveObjC icon indicating copy to clipboard operation
ReactiveObjC copied to clipboard

RACObserve macro doesn't support class property observation

Open tepmnthar opened this issue 8 years ago • 0 comments

I have a class with class property

@interface MYClass
@property (class, nonatomic, readonly) clsProp;
@end

And I want to observe clsProp by using RACObserve(MYClass, clsProp). But it will get an error, because the preprocessed code is something like this:

    ({
        __attribute__((objc_ownership(weak))) id target_ = (MYClass); // ERROR
        [target_ rac_valuesForKeyPath:@(((void)(__objc_no && ((void)MYClass.clsProp, __objc_no)), "clsProp")) observer:self];
    });

Also I can't using RACObserve(MYClass.class, clsProp), because MYClass.class.clsProp in the second line will fail this time.

So, I need a RACObserve which support class property observation. My solution is add another RACClassObserve macro like this:

#if __clang__ && (__clang_major__ >= 8)
#define RACClassObserve(TARGET, KEYPATH) _RACClassObserve(TARGET, KEYPATH)
#else
#define RACClassObserve(TARGET, KEYPATH) \
({ \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wreceiver-is-weak\"") \
_RACClassObserve(TARGET, KEYPATH) \
_Pragma("clang diagnostic pop") \
})
#endif

#define _RACClassObserve(TARGET, KEYPATH) \
({ \
__weak id target_ = (TARGET.class); \
[target_ rac_valuesForKeyPath:@keypath(TARGET, KEYPATH) observer:self]; \
})

But I think it's really not elegant.

tepmnthar avatar Oct 26 '17 05:10 tepmnthar