RACSubscriptingAssignmentTrampoline.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // RACSubscriptingAssignmentTrampoline.h
  3. // ReactiveCocoa
  4. //
  5. // Created by Josh Abernathy on 9/24/12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <ReactiveCocoa/EXTKeyPathCoding.h>
  10. @class RACSignal;
  11. /// Assigns a signal to an object property, automatically setting the given key
  12. /// path on every `next`. When the signal completes, the binding is automatically
  13. /// disposed of.
  14. ///
  15. /// There are two different versions of this macro:
  16. ///
  17. /// - RAC(TARGET, KEYPATH, NILVALUE) will bind the `KEYPATH` of `TARGET` to the
  18. /// given signal. If the signal ever sends a `nil` value, the property will be
  19. /// set to `NILVALUE` instead. `NILVALUE` may itself be `nil` for object
  20. /// properties, but an NSValue should be used for primitive properties, to
  21. /// avoid an exception if `nil` is sent (which might occur if an intermediate
  22. /// object is set to `nil`).
  23. /// - RAC(TARGET, KEYPATH) is the same as the above, but `NILVALUE` defaults to
  24. /// `nil`.
  25. ///
  26. /// See -[RACSignal setKeyPath:onObject:nilValue:] for more information about the
  27. /// binding's semantics.
  28. ///
  29. /// Examples
  30. ///
  31. /// RAC(self, objectProperty) = objectSignal;
  32. /// RAC(self, stringProperty, @"foobar") = stringSignal;
  33. /// RAC(self, integerProperty, @42) = integerSignal;
  34. ///
  35. /// WARNING: Under certain conditions, use of this macro can be thread-unsafe.
  36. /// See the documentation of -setKeyPath:onObject:nilValue:.
  37. #define RAC(TARGET, ...) \
  38. metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
  39. (RAC_(TARGET, __VA_ARGS__, nil)) \
  40. (RAC_(TARGET, __VA_ARGS__))
  41. /// Do not use this directly. Use the RAC macro above.
  42. #define RAC_(TARGET, KEYPATH, NILVALUE) \
  43. [[RACSubscriptingAssignmentTrampoline alloc] initWithTarget:(TARGET) nilValue:(NILVALUE)][@keypath(TARGET, KEYPATH)]
  44. @interface RACSubscriptingAssignmentTrampoline : NSObject
  45. - (id)initWithTarget:(id)target nilValue:(id)nilValue;
  46. - (void)setObject:(RACSignal *)signal forKeyedSubscript:(NSString *)keyPath;
  47. @end